Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Lazy) LEFT OUTER JOIN using the Hibernate Criteria API

Tags:

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

like image 594
Markos Fragkakis Avatar asked Feb 05 '10 11:02

Markos Fragkakis


People also ask

How do you write join query in Hibernate using criteria?

Criteria in Hibernate can be used for join queries by joining multiple tables, useful methods for Hibernate criteria join are createAlias(), setFetchMode() and setProjection() Criteria in Hibernate API can be used for fetching results with conditions, useful methods are add() where we can add Restrictions.

What is Hibernate Criteria API?

Advertisements. Hibernate provides alternate ways of manipulating objects and in turn data available in RDBMS tables. One of the methods is Criteria API, which allows you to build up a criteria query object programmatically where you can apply filtration rules and logical conditions.

What is left join fetch in Hibernate?

JOIN FETCH (or LEFT JOIN FETCH ) will collect all the associations along with their owner object. Meaning that the collection will be retrieved in the same select. This can be shown by enabling Hibernate's statistics. A (left/outer) join fetch is great for *ToOne (many-to-one or one-to-one) associations.


1 Answers

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

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

like image 173
sdavids Avatar answered Sep 19 '22 15:09

sdavids