(惰性)使用 Hibernate Criteria API 的 LEFT OUTER JOIN
我想使用 Criteria API 在两个表之间执行 LEFT OUTER JOIN。我在 Hibernate 文档中能找到的就是这个方法:
Criteria criteria = this.crudService
.initializeCriteria(Applicant.class)
.setFetchMode("products", FetchMode.JOIN)
.createAlias("products", "product");
但是,由于它返回的结果数量,这要么执行内部联接,要么执行右外部联接。
我也希望我的加入是惰性的。我该怎么做?
干杯!
更新:似乎使用别名可以自动连接 INNER JOIN 。 “背景故事”中有一些我还没有掌握的东西。所以,今天没有别名。这给我留下了对两个表应用限制的问题,因为它们都有一个列(或属性,如果这更合适)“name”。
I want to perform a LEFT OUTER JOIN between two tables using the Criteria API. All I could find in the Hibernate documentation is this method:
Criteria criteria = this.crudService
.initializeCriteria(Applicant.class)
.setFetchMode("products", FetchMode.JOIN)
.createAlias("products", "product");
However, this either performs an inner join or a right outer join, because of the number of results it returns.
I also want my join to be Lazy. How can I do this?
Cheers!
UPDATE: It seems that using aliases makes the join INNER JOIN automatically. There is something in the "background story" I have not grasped yet. So, no alias today. This leaves me with the problem of applying restrictions to the two tables, because they both have a column (or property, if this is more appropriate) 'name'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要在产品表上左连接,只需执行以下操作:
If you need to left join on the products table just do:
Sdavids 答案的 API 现已
弃用
。其更新版本为Sdavids answer's API is
deprecated
now. Its updated version isSQL 请求中包含联接。它不能偷懒。
使用 Hibernate,要延迟检索此数据,只需将其从 HQL 请求中排除即可。然后,当您访问实体上的 getter 时(如果您的会话仍然打开),它将自动加载(您不必编写请求的该部分)。
A join is in the SQL request. It can't be lazy.
With Hibernate, to retrieve lazily this data, just exclude it from the HQL request. Then, when you access the getters on your entity (if your Session is still open), it will be loaded automatically (you don't have to write that part of the request).