Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Hibernate创建条件连接同一个表两次-尝试了2种方法,但有2个不同错误_Sql_Oracle_Hibernate_Criteria - Fatal编程技术网

Sql Hibernate创建条件连接同一个表两次-尝试了2种方法,但有2个不同错误

Sql Hibernate创建条件连接同一个表两次-尝试了2种方法,但有2个不同错误,sql,oracle,hibernate,criteria,Sql,Oracle,Hibernate,Criteria,我想为以下本机sql创建条件 不幸的是,我在两次使用createCriteria时遇到了重复关联路径的错误。 当我尝试使用Restrictions.sqlRestriction时。它无法提供我想要的SQL 尝试1:创建条件-重复关联路径 Criteria criteria = getSession().createCriteria( Company.class ); criteria.createAlias( "customerCategories", "c1

我想为以下本机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”而导致关系运算符无效

错误结果

预期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问题上有一个旧的Hibernate错误:重复的关联路径
已于2005年打开,但仍然打开

另一个问题没有解决

因此,选项1)不太合适

但是在上述bug的注释中提到了一个有用的解决方法,即使用
exists

因此,将
sqlRestriction
exists
一起使用两次,并使用相关子查询过滤propper类别。您将只获得连接到这两个类别的公司

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转换为使用条件?发布实体源代码。阅读如何正确发布问题:
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 = ?)