Hibernate Create Criteria两次加入同一个表 - 尝试了2个方法,带有2个差异错误

问题描述 投票:1回答:1

我想为以下本机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

感谢您的帮助。谢谢。

sql oracle hibernate criteria
1个回答
1
投票

有一个旧的Hibernate bug HHH-879关于org.hibernate.QueryException: duplicate association path 2005年开放的问题仍然开放...

其他问题没有解决方案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 ) ); 以及过滤propper类别的相关子查询。您将只获得与这两个类别相关的公司。

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 = ?)

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

qazxswpoi
© www.soinside.com 2019 - 2024. All rights reserved.