简体   繁体   English

休眠条件:休眠左联接而无关联

[英]Hibernate Criteria: hibernate left join without association

I am a Hibernate newbie and i have this below query. 我是Hibernate新手,下面有这个查询。 It is working as i expected. 它按我的预期工作。 These two tables are not associated. 这两个表未关联。 Is there a way to get the same result by using Criteria API or how can i run this query via Hibernate ? 有没有一种方法可以通过使用Criteria API来获得相同的结果,或者如何通过Hibernate运行此查询? Any help would be appreciated. 任何帮助,将不胜感激。

SELECT p.title, c.content
  FROM posts p
  LEFT JOIN comments c ON p.id = c.post_id
  WHERE p.status = 'A' AND (p.title iLIKE '%r%' OR c.content iLIKE '%r%');

Criteria API needs a path between entities, so I'm not sure this join could be done using Criteria API. Criteria API需要实体之间的路径 ,因此我不确定可以使用Criteria API来完成此连接。 Better do it with HQL if you have Hibernate >= 5.1: 如果您的Hibernate> = 5.1,最好使用HQL

select p.title, c.content
from org.example.Posts p
left outer join org.example.Comments c
on p.id = c.id
where p.status = 'A' AND (lower(p.title) LIKE '%r%' OR lower(c.content) LIKE '%r%');

Still, you could stick to using SQL queries with Hibernate or better still, create association between Posts and Comments. 尽管如此,您仍可以继续在Hibernate中使用SQL查询,或者更好的方法是在Posts和Comments之间创建关联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM