-->
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: Filtering association data
PostPosted: Mon Jan 07, 2008 10:29 am 
Newbie

Joined: Mon Jan 07, 2008 9:51 am
Posts: 3
Hi.

I have two related objects, Account and LineItem. See end of this post for hibernate mapping data. In "view" type situations I would like to filter the LineItems associated with an account by date (for example, LineItems between 01/01/2000 and 01/02/2000).

I have tried various methods using both HQL and Criteria. I can succesfully filter the LineItems, however I can only get the LineItems back as a new List, not as an update collection within a populate Account object. Is it possible to filter an association in place?

Just to make it clear, my full data set could be as follows:

Account.name=Account1
Account.type=Savings
Account.lineItem.1.description=Cash
Account.lineItem.1.postingDate=01/01/2000
Account.lineItem.2.description=Food
Account.lineItem.2.postingDate=05/01/2000
Account.lineItem.3.description=Books
Account.lineItem.3.postingDate=01/02/2000

for my view I would like to receive an Account that only has LineItems between 01/01/2000 and 01/02/2000 (even thought the database has more LineItems).

Account.name=Account1
Account.type=Savings
Account.lineItem.1.description=Cash
Account.lineItem.1.postingDate=01/01/2000
Account.lineItem.2.description=Food
Account.lineItem.2.postingDate=05/01/2000


Code:
<class name="Account" table="FINANCE_ACCOUNTS">
    <id name="accountID">
        <generator class="increment"/>   
    </id>
    <property name="name"/>
    <property name="type" type="accountType"/>
       
    <set name="lineItems" order-by="postingDate desc">
      <key column="accountId"/>
      <one-to-many class="LineItem"/>
    </set>
      
    <many-to-one name="user" column="userid"/>       
</class>
<class name="LineItem" table="FINANCE_LINEITEM">
    <id name="lineItemId">
        <generator class="increment"/>         
    </id>
    <property name="description"/>
    <property name="amount"/>
    <property name="postingDate"/>
      
    <many-to-one name="account" column="accountId"/>
</class>   


Many thanks for any help given.


Top
 Profile  
 
 Post subject: Re: Filtering association data
PostPosted: Mon Jan 07, 2008 12:58 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
herbert912 wrote:
Hi.

I have two related objects, Account and LineItem. See end of this post for hibernate mapping data. In "view" type situations I would like to filter the LineItems associated with an account by date (for example, LineItems between 01/01/2000 and 01/02/2000).



If I understand your problem correctly you need to retrieve a parent which has a child list which needs to be filtered in certain use cases. Imagine you have a classic person address relation. The following HQL will retrieve all persons with specific addresses and it only returns the matched addresses even though a person might have more addresses in database:


Code:
select p from Person p inner join fetch p.address ad where ad.addressline = :address


I hope it helps.

Farzad-


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.