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'.
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.
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.
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.
If you need to left join on the products table just do:
.....createAlias("products", "product", Criteria.LEFT_JOIN);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With