当前位置: 首页 > 面试题库 >

Hibernate Create Criteria两次连接同一张表-尝试了2种方法,但有2个差异错误

孔茂
2023-03-14
问题内容

我想为以下本机sql创建创建条件。

不幸的是,我两次使用createCriteria时遇到重复的关联路径错误。当我尝试使用Restrictions.sqlRestriction时。它无法提供我想要的SQL。

尝试1:创建条件-重复的关联路径

 Criteria criteria = getSession().createCriteria( Company.class );
                 criteria.createAlias( "customerCategories", "c1" );
                 criteria.add( Restrictions.in( "c1.customerCategory.customerCategoryId",
                         company.getBaseCustomerCategoryId() ) );
                 criteria.createAlias( "customerCategories", "c2" );
                 criteria.add( Restrictions.in( "c2.customerCategory.customerCategoryId",
                         company.getPromoCustomerCategoryId() ) );

尝试2:创建SQL限制-ORA-00920:由于“ where”,关系运算符无效

  Criteria criteria = getSession().createCriteria( Company.class );
                 criteria.add( Restrictions.sqlRestriction(
                         "INNER JOIN Company_Customercategory a on {alias}.companyId = a.companyId and a.CUSTOMERCATEGORYID = ?",
                         company.getBaseCustomerCategoryId(), LongType.INSTANCE ) );
                 criteria.add( Restrictions.sqlRestriction( 
                         "1=1 INNER JOIN Company_Customercategory b on {alias}.companyId = b.companyId
 and b.CUSTOMERCATEGORYID = ?", 
                         company.getPromoCustomerCategoryId(), LongType.INSTANCE) );

结果错误

select this_.* from Companies this_ where 
  INNER JOIN Company_Customercategory a 
  on this_.companyId = a.companyId 
  and a.CUSTOMERCATEGORYID = 1
  and 1=1 INNER JOIN Company_Customercategory b 
  on this_.companyId = b.companyId 
  and b.CUSTOMERCATEGORYID = 6

预期的SQL

select * from companies c
  inner join Company_Customercategory a
  on c.companyId = a.companyId
  and a.CUSTOMERCATEGORYID = 1
  inner JOIN Company_Customercategory b
  on a.companyId = b.companyId
  and b.CUSTOMERCATEGORYID = 6

感谢你的帮助。谢谢。


问题答案:

关于2005年打开的问题,还有一个旧的Hibernate错误HHH-879org.hibernate.QueryException: duplicate association path仍在打开…

其他问题已关闭,但没有解决方案HHH-7882

因此,选项1)不太适合。

但是,在上述错误的意见的有用 的解决方法 是使用提及exists

因此,请使用两次sqlRestrictionexists并使用一个相关的子查询来过滤属性类别。您将获得唯一的 企业 连接到这两个类别。

crit.add( Restrictions.sqlRestriction( 
  "exists (select null from Company_Customercategory a where {alias}.company_Id = a.company_Id and a.CUSTOMERCATEGORYID = ?)",
  1, IntegerType.INSTANCE ) );
crit.add( Restrictions.sqlRestriction( 
  "exists (select null from Company_Customercategory a where {alias}.company_Id = a.company_Id and a.CUSTOMERCATEGORYID = ?)",
  6, IntegerType.INSTANCE ) );

这导致以下查询提供正确的结果

select this_.COMPANY_ID as COMPANY_ID1_2_0_, this_.COMPANY_NAME as COMPANY_NAME2_2_0_ 
from COMPANIES this_ 
where exists (select null from Company_Customercategory a 
              where this_.company_Id = a.company_Id and a.CUSTOMERCATEGORYID =  ?) and 
      exists (select null from Company_Customercategory a 
              where this_.company_Id = a.company_Id and a.CUSTOMERCATEGORYID = ?)


 类似资料:
  • 问题内容: 我想为以下本机sql创建创建条件。 不幸的是,我两次使用createCriteria时遇到重复的关联路径错误。当我尝试使用Restrictions.sqlRestriction时。它无法提供我想要的SQL。 尝试1:创建条件-重复的关联路径 尝试2:创建SQL限制-ORA-00920:由于“ where”,关系运算符无效 结果错误 预期的SQL 感谢你的帮助。谢谢。 问题答案: 关于2

  • 问题内容: 我有一张桌子和一张桌子。 该表具有以下结构: 所有用户( 投诉者 和 投诉解决者) 都位于table中。 如何编写查询以显示两列的用户名? 这给了我一个: 但我不知道如何编写,因此两列均显示用户名而不是ID。 问题答案:

  • 问题内容: 因此,我收到此错误:#1066-不是唯一的表/别名:“购买” 我正在尝试执行以下操作: 空白表包括: 付款表包括: 采购表包括: 票务表包括: MCO_Blank表包含: 我不确定如何进行这项工作。 问题答案: 您需要使用表别名。您在子句中多次提到同一张表。查询是这样的: 我不得不猜测别名应该使用哪种付款方式和购买方式。这些在and子句中可能不正确。

  • 问题内容: 我有2张桌子。一个(域)具有域ID和域名(dom_id,dom_url)。 另一个包含实际数据,其中2列需要TO和FROM域名。所以我有2列rev_dom_from和rev_dom_for,它们都存储来自domains表的域名ID。 简单。 现在,我需要在网页上实际显示两个域名。我知道如何通过LEFT JOIN域名显示reviews.rev_dom_for = domains.dom_

  • 问题内容: 我有两个桌子和。表具有字段和其他几个字段。表具有3个字段: 是固定的值枚举。例如,它可以是,或。 我需要进行查询,其结果将是4列: ,对,对,对 如果不存在用于相应值在表然后NULL值应在相应的列中显示。 例: 结果应该是: 我尝试了3次左联接表,但无法弄清楚如何排列输出 问题答案: 您需要使用多个: 这是一个示例:SQL Fiddle。

  • 问题内容: 假设我有一个包含200列的表格,其中大多数从未使用过。 我将SmallEntity映射到经常使用的10列。我在与其他实体的关联中使用它。它加载速度快,消耗很少的内存,让我很高兴。 但是有时我需要显示200列。我想在200列上映射BigEntity类。它没有绑定到其他实体,也没有关联。 问题:您有这样做的经验吗?您是否知道Hibernate可能会遇到的任何麻烦,例如在一级缓存,脏检查和实