Hello,
I have the following situation: I have a parent table (say, OWNER), and a child table (say, CAR). I'd like to select all owners who have red honda civic, and only those children that describe a red honda civic entry.
Here's my Hibernate hierarchy:
Owner { String name; Set<Car> cars; } //Car class has many-to-one link to Owner
Car { String model; String make; String color; }
When I try to do something like this:
Criteria c=session.createCriteria(Owner.class);
c.add(Expression.eq("name","John"));
c.createAlias("cars","crs");
c.add(Expression.eq("crs.model","Civic"));
c.add(Expression.eq("crs.make","Honda"));
c.add(Expression.eq("crs.color","red"));
This query returns Owner objects for owners that have red honda civic cars, but each Owner object contains more than just the red Honda Civic cars.
I'm already using DISTINCT_ROOT_ENTITY result transformer to get unique Owners. Is there something I can turn on to get only those children in cars collection where Car objects match 'red honda civic' criteria?
I was reading about filters, but it looks like those have to be defined in mapping files, and they are pretty much static. I'm looking for more generic approach that would work for Owner/Car tables, and for other ones. Is there some setting in Hibernate that I could turn on to get only those child records that match the criteria?
It seems a bit unnatural that collections of child objects (like Owner.cars in my case) contain objects that don't match the search criteria, when search criteria were specified for the child table (CAR).
Anyway, I'd appreciate any help on the subject.
Thank you,
Bratek
|