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 can't chain properties in a criteria query. And even in a HQL queries, cars.sales
is invalid, since cars
refers to a collection, and not to an instance of Car. A join is necessary to be able to refer to a Car in the collection of cars. That's what the createAlias()
call does. The above is similar to
select showRoom from ShowRoom
left join fetch showRoom.cars car
left join fetch car.sales