-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Using two-level filtering on 1:n associations with Criteria
PostPosted: Wed Oct 17, 2007 9:26 pm 
Newbie

Joined: Tue Mar 27, 2007 2:00 pm
Posts: 4
I have the following scenario that I am trying to solve using Hibernate as the persistence provider, Hibernate Criteria API to build my queries.

I have two classes called Order and Item with 1:n unidirectional association from Order->Item. Both are JPA/Hibernate entities and are mapped to ORDER and ITEM table in the database respectively.

In my application, I have a search screen which allows the user to put an item price in a text field. The search results should only include those Orders that have at least one item with price < specified price. Another requirement is that since an Order can have multiple items, it is possible that some items on the Order can have price > specified price. In that case, the Order should be selected but its items collection should not contain all items associated with that order but those items with price < specified price.

For example:

I have two orders Order1: Item 1: price 100, Item 2: price 200.
Order 2: Item 3: price 50, Item 4 price 40

Now, if the query says price < 150, then, the result set should be:
Order 1 with its items collection only containing Item 1. Item 2 is taken out since its price is 200 which is > 150.
Order 2 with its items collection containing both Item 3 and Item 4.

I am using HIbernate Criteria API to build my search query. I am setting the fetch type for the items association on Order to EAGER. WHen I execute the crieria.list() (criteria already has the price < 150 criterion added to it), I get following queries:

select order.id, item.... from order, item where order.order_id = item.order_id and item.price < 150.

This query returns the desired result set. Unfortunately, Hibernate then runs another query in addition to the query above where it gets:

select item...from item where item.order_id=?
and so, it puts the 2nd item (Item 2) also in Order 1.

I am trying to understand why Hibernate is executing the 2nd query and thus, ends up loading the second Item (Item 2) on Order 1. Could this be because of the fetch mode?

In a way, I am trying to use Hibernate Criteria API to do a two-level filtering: one on the Order objects and then, on individual Items within the Order object.

I am thinking that Hibernate Criteria API does not apply to two-level filtering and so, I will have to use a Hibernate filter after criteria.list() to filter out Item 2 from Order 1.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 22, 2007 11:51 am 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Put the filter condition on your 1 to many as well. The class level filter is only applying to the initial select. The collection level filter will then apply when hibernate tries to load the items.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.