Hibernate Create Criteria 两次加入相同的 table - 尝试了 2 种方法,但有 2 种差异错误
Hibernate Create Criteria to join the same table twice - tried 2 approach with 2 difference error
我想为以下本地人创建创建条件 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
感谢您的帮助。
谢谢。
关于 org.hibernate.QueryException: duplicate association path
opened 2005 and still open...
的问题,存在一个旧的 Hibernate bug HHH-879
其他问题未解决已关闭HHH-7882
所以选项1)不太合适
但是在上述错误的评论中,使用 exists
提到了一个有用的 解决方法
所以使用两次 sqlRestriction
和 exists
以及一个相关的子查询过滤适当的类别。您只会获得与这两个类别相关联的 公司。
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:创建条件 - 重复关联路径
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
感谢您的帮助。 谢谢。
关于 org.hibernate.QueryException: duplicate association path
opened 2005 and still open...
其他问题未解决已关闭HHH-7882
所以选项1)不太合适
但是在上述错误的评论中,使用 exists
所以使用两次 sqlRestriction
和 exists
以及一个相关的子查询过滤适当的类别。您只会获得与这两个类别相关联的 公司。
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 = ?)