This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Hibernate: unexpected queries in parent/child relationship Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate: unexpected queries in parent/child relationship" Watch "Hibernate: unexpected queries in parent/child relationship" New topic
Author

Hibernate: unexpected queries in parent/child relationship

Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
Could someone point out what I'm doing wrong? I have a parent/child relationship between two objects, Customer and Order - a customer can have many orders. The tables are setup the same way and the two join on a pk/fk field called 'cust_id'.

So...it looks like this:

Customer-<Order

Customer.Orders = List<Order> (mapped as a bag, like so):



I'm trying to get a list of customers between an Order sent-date range. The customers I get back are ones that have orders within the date range but when I try to view the list of orders...they're not filtered...I'm getting every order ever entered by that customer...I would have expected the orders to be filtered as well...but they're not...and that's obviously because of the queries being generated.

My HQL looks like this:



...which generates a query that looks like this:



However...when I list the orders for each customer...like so:



It generates a query that looks like this:



...bingo...the Order.SentDate isn't filtered for the orders.

I know I'm missing something simple...but I've been staring at it for so long it's eluding me...can someone point out a simple work-around?
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
This is by design.

The order date criteria is just used to filter the Customer objects themselves.

Once you have a Customer, calling getOrders() will retrieve all orders for that Customer. The collections inside the Customer objects are not filtered based on your original query criteria.

Try using a collection Filter. Also see Hibernate in Action section 7.5.2.
Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
Yep, that's exactly what I ended up doing after flipping through the "report queries" chapter in Hibernate In Action.

Sure would be nice if you could do this at the top-level w/o having to again filter the data a second time!

Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate: unexpected queries in parent/child relationship
 
Similar Threads
Optimisation problem
Mapping a qualified association with Hibernate
Designing in Hibernate
Hibernate - id column of an entity as list-index column in another entity?
What does hibernate think is "dirty" here?