-->
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.  [ 3 posts ] 
Author Message
 Post subject: Unexpected queries in parent/child relationship
PostPosted: Mon Jan 09, 2006 6:02 pm 
Regular
Regular

Joined: Mon Nov 14, 2005 7:33 pm
Posts: 73
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):

Code:
<hibernate-mapping>
   <class name="com.portlets.bol.Customer" table="ps_customer">
     <id name="Id" column="cust_id" type="long" unsaved-value="0">
      <generator class="identity" />
    </id>
     <property name="Name" column="name1" />
     <bag
        name="Orders"
        table="sr_order"
        lazy="true"
        fetch="join"
         inverse="true"
        cascade="all-delete-orphan">
        <key column="cust_id" />
        <one-to-many class="com.portlets.bol.Order" />
     </bag>
   </class>
</hibernate-mapping>


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:

Code:
List<Customer> customers = s.createQuery("from Customer c where c.Orders.SentDate between :bd and :ed")
   .setDate("bd", startDate)
   .setDate("ed", endDate)
   .list();


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

Code:
select customer0_.cust_id as cust1_9_, customer0_.name1 as name2_9_ from ps_customer customer0_, sr_order orders1_ where customer0_.cust_id=orders1_.cust_id and (orders1_.sent_date between ? and ?)


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

Code:
List<Order> orders = customers.getOrders();
         
for (Order o : orders)
{
      response.getWriter().println("&nbsp;&nbsp&nbsp;&nbsp&#149 " + o.getId() + "<br />");
}


It generates a query that looks like this:

Code:
select orders0_.cust_id as cust4_1_, orders0_.order_id as order1_1_, orders0_.order_id as order1_10_0_, orders0_.transporter_id as transpor2_10_0_, orders0_.inv_pick_list_id as inv3_10_0_, orders0_.cust_id as cust4_10_0_, orders0_.order_date as order5_10_0_, orders0_.sent_date as sent6_10_0_, orders0_.to_name as to7_10_0_, orders0_.to_addr1 as to8_10_0_, orders0_.to_addr2 as to9_10_0_, orders0_.to_city as to10_10_0_, orders0_.to_state as to11_10_0_, orders0_.to_country as to12_10_0_, orders0_.to_postalcode as to13_10_0_ from sr_order orders0_ where orders0_.cust_id in (?, ?, ?, ?, ?)


...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?

Hibernate version: 3.1

Name and version of the database you are using: MSSQL 2000


Top
 Profile  
 
 Post subject: Re: Unexpected queries in parent/child relationship
PostPosted: Wed Jan 11, 2006 10:09 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
tsar bomba wrote:

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

That's the expected behaviour, a filtered list of customers that have orders in the given range :)

tsar bomba wrote:
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.

Obviously. And you also get the obviously expected result.
You asked for a list of customers having orders in a given range. You got it. Then you take a customer and request all it's orders using getOrders(), you get it. The filtering is done on customers, not on customers.orders.
Hibernate will never returns you a partial Customer (that is with only a subset of order in it's order set) unless stated in a static filtering rule inside your .xml mapping file.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 12:15 pm 
Regular
Regular

Joined: Mon Nov 14, 2005 7:33 pm
Posts: 73
It's cool, I found a way around it - collection filters.

Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.