I would like to create create criteria for the following native sql.
Unfortunately, I hit error of duplicate associate path when use createCriteria twice.
When I try to use Restrictions.sqlRestriction. It unable provide the SQL that I want.
Try 1: Create Criteria - duplicate associate path
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() ) );
Try 2: Create SQL Restriction - ORA-00920: invalid relational operator because of "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) );
Wrong Result
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
Expected 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
Appreciate your help.
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…