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