I need to modify a large Hibernate DetachedCriteria query to get some additional associations.
My graph of objects resembles the structure below, I want to get sales associated with each car:
<class name="Showroom" table="showroom"> ... <bag name="cars" lazy="false"> <one-to-many class="Car" /> </bag> ... </class> <class name="Car" table="car"> ... <set name="sales" lazy="true"> <one-to-many class="Sale" /> </set> ... </class>
I want to do something like:
DetachedCriteria criteria = DetachedCriteria.forClass(Showroom.class); // ... Existing criteria query code ... criteria.setFetchMode("cars.sales", FetchMode.JOIN);
But the associationPath argument in setFetchMode seems to ignore dot notation, and I get a LazyInitializationException:
Called: org.hibernate.LazyInitializationException: failed to lazily initialize role collection: Car.sales, session or session not closed
I searched around and still have not found examples or information. The Hibernate documentation does not contain examples of how to extract nested associations, and the Javadoc for setFetchMode seems to indicate that my point note approach should work ...
Any help would be appreciated.
seanhodges
source share