(Lazy) LEFT OUTER JOIN using the Hibernate Criteria API

谁说胖子不能爱 提交于 2019-11-30 12:47:16

问题


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'.


回答1:


If you need to left join on the products table just do:

.....createAlias("products", "product", Criteria.LEFT_JOIN);




回答2:


Sdavids answer's API is deprecated now. Its updated version is

....createAlias("employee", "emp", JoinType.LEFT_OUTER_JOIN)




回答3:


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).



来源:https://stackoverflow.com/questions/2206761/lazy-left-outer-join-using-the-hibernate-criteria-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!