java - hibernate 标准中 LEFT_JOIN 中的 OR 子句

标签 java mysql sql hibernate

如何在 hibernate 条件中的 LEFT_JOIN 中提供 OR 子句。

Criteria mainCrit=hibernateSession.createCriteria("Main.class");    
Criteria aPropertyCrit=mainCrit.createCrieria("child",CriteriaSpecification.LEFT_JOIN);

它生成的sql为

select this_.Id,this_childId....
from main as this_
left join child as child1_ on child1_ .id=this_.childId
where.....

我需要生成 sql 为

select this_.Id,this_childId....
from main as this_
left join child as child1_ on child1_.id=this_.childId or child_.ParentId=this_.childId
where.....

如何在上述条件的 LEFT_JOIN 中提供 OR 子句。

最佳答案

即使使用 Criteria API,也可以扩展 JOIN ... ON 部分。但只是为了更具限制性,即添加 AND 部分。

看看 createCriteria 的重载方法,主要是最后一个参数withClause:

Criteria createCriteria(String associationPath,
                        String alias,
                        JoinType joinType,
                        Criterion withClause)
                        throws HibernateException

创建一个新的条件,“ Root ”于关联实体,分配给定的别名并使用指定的连接类型。

参数:

  • associationPath - 以点分隔的属性路径
  • alias - 分配给已加入关联的别名(供以后引用)。
  • joinType - 要使用的连接类型。
  • withClause - 要添加到连接条件(ON 子句)的条件

事实上,任何其他方法(例如添加 OR) 都破坏了映射原则,引入了在末尾进行交叉连接的能力。请参阅Hibernate criteria: Joining table without a mapped association

关于java - hibernate 标准中 LEFT_JOIN 中的 OR 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19015498/

相关文章:

java - 运行 JNI native 代码时的 JRE SIGSEGV

java - 许多相同的按钮代码(简短的然后)

php - 如何在不删除或移动mysql中的表的情况下重命名数据库?

sql - 如何在 Oracle 中重置序列?

mysql - FROM 子句中的子查询在 MySQL 中失败

java - 通过 JournalStructureLocalServiceUtil Liferay 获取结构名称

Java - 如何过滤值(列表)

PHP : Return JSON content instead of String from database

mysql - 如何使用 MySQL 设置 Bolt CMS?

mysql - 是否每个 SQL 都在未到达 COMMIT 点时撤消?