23

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

Mikko Maunu
42.2k11 gold badges143 silver badges142 bronze badges
asked Feb 5, 2010 at 11:20
2
  • 2
    Accept the answer already. Commented Apr 30, 2013 at 21:05
  • I want to know you reference of "createAlia() in Hibernate by default creates a INNER JOIN". I have searched API and cannot see it. Thanks. Commented Mar 11, 2016 at 11:33

3 Answers 3

39

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

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

answered Feb 6, 2010 at 4:49
Sign up to request clarification or add additional context in comments.

1 Comment

Deprecated now, use Ankit's answer
27

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

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

answered Feb 28, 2015 at 8:21

1 Comment

Thanks. It helps me to eliminate usage of deprecated API
0

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

answered Feb 5, 2010 at 11:22

4 Comments

Sorry, I didn't get this. Perhaps I should rephrase: I want to have a LEFT OUTER JOIN, but also I want the data to be actually retrieved only when needed (the appropriate getter is called). Is Laziness (or eagerness) somehow connected with the type of SQL JOIN that takes place?
Lazyness may be realised with objects, but not with an SQL request. A request is executed once fully, it is not designed so that part of the request is executed later if needed!
In HQL I write LEFT OUTER JOIN FETCH. So, presumably an HQL query corresponds to many SQL ones, which are executed when needed? Anyway, lazily or not, how can I do a LEFT OUTER JOIN? And not in HQL, using the Criteria API. Cheers!
@Markos For joins, the HQL query get translated to only one equivalent SQL query. For the Criteria part, I'm not the expert... :-)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.