Unwanted selection of a nested object using DetachedCriteria in sleep mode

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.

+7
source share
1 answer
 DetachedCriteria dc = DetachedCriteria.forClass(ShowRoom.class, "showRoom"); criteria.setFetchMode("showRoom.cars", FetchMode.JOIN); criteria.createAlias("showRoom.cars", "car", CriteriaSPecification.LEFT_JOIN); criteria.setFetchMode("car.sales", FetchMode.JOIN); 

You cannot bind properties in query criteria. And even in HQL queries, cars.sales is invalid because cars refers to a collection, not a Car instance. Joining is necessary in order to be able to reference the Car in the car collection. This is what the createAlias() call createAlias() . The above is similar to

 select showRoom from ShowRoom left join fetch showRoom.cars car left join fetch car.sales 
+15
source

All Articles