-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Criteria with multiple joins
PostPosted: Mon Nov 26, 2007 2:49 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
This is a general question about the inner workings of Hibernate.
Suppose I have 3 tables: Countries, Provinces and Municipalities.
Each one has a one-to-many relationship to the next one.
All data is normalized, there are no orphan records anywhere.

In order to build a criteria, I use the following code.

Code:
Criteria crit=session.createCriteria(Country.class);
crit.createAlias("provinces", "pr").
createAlias("municipalities", "mun")
.add( ... restrictions ...);


The previous code works only if restrictions apply to the main class (Country), but if a restriction apply to one of the child tables, say, Municipality, when the values are retrieved a LazyInitializationException is thrown.


The following code solves the problem:

Code:
Criteria crit=session.createCriteria(Country.class)
.createCriteria("provinces", Criteria.LEFT_JOIN)
.createCriteria("municipalities", Criteria.LEFT_JOIN)
.add(... restrictions on Municipalities ...)


Could anyone explain to me why a LEFT JOIN is necessary, when all the data is normalized?

Thanks in advance

Gonzalo Díaz


Top
 Profile  
 
 Post subject: Solved
PostPosted: Mon Dec 24, 2007 1:01 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
I withdraw this question.
There is nothing inherent to the Criteria that forces a LEFT JOIN on the children.
My problem was that, along with the children (Provinces) of "Countries", I had several other one-to-many associations issuing from the main table.
This caused nulls to be returned on the right side of my resultset.
The only way a consistent resultset can be returned in this case, is by using LEFT JOINS all the way.

Gonzalo Díaz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.