-->
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.  [ 5 posts ] 
Author Message
 Post subject: loading only part of a collection
PostPosted: Tue Nov 18, 2003 11:50 am 
Newbie

Joined: Tue Nov 18, 2003 11:30 am
Posts: 2
If I would have the following mapping:

public class Wallet {
private int id;
private List transfers;
// getters and setters
....
}

public class Transfer {
private int id;
private int walletId;
private long timestamp;
// getters and setters
....
}

<hibernate-mapping>
<class name="Wallet" table="wallet">
<id name="id" type="int" column="id">
<generator class="assigned"/>
</id>
<bag name="transfers" table="transfer" inverse="true" cascade="save-update" >
<key column="wallet_id"/>
<one-to-many class="Transfer"/>
</bag>
<class>

<class name="Transfer" table="transfer">
<id name="id" type="int" column="id">
<generator class="assigned"/>
</id>
<property name="walletId" column="wallet_id" type="int"/>
<property name="timestamp" column="timestamp" type="long"/>
</class>
</hibernate-mapping>


Now I want to retrieve the wallet but only with the transfer collection to consist of all the tranfers within a certain time period (select on timestamp). Additionally if no transfers within that period can be found I just want the wallet with an empty collection.

What query should I use ?

Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2003 5:30 pm 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
I don't think this is possible to specify in the mapping unless your date range is always the same. (Or if your date range is contained within each Wallet instance it would also be possible.) Otherwise, I think the only way to do this is with a runtime HQL query.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 18, 2003 8:00 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
You always can make a separate method (additional to "standard" getTransfers getter) like getTransfers(Date, Date) which retrieves only part of collection using collection filter (session.filter(...))


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 7:56 am 
Newbie

Joined: Tue Nov 18, 2003 11:30 am
Posts: 2
dimas wrote:
You always can make a separate method (additional to "standard" getTransfers getter) like getTransfers(Date, Date) which retrieves only part of collection using collection filter (session.filter(...))


Thanks, this worked partially. I now get transfers within a give time period but if I look at sql hibernate executes it first retrieves the wallet, then selects all the transfers and only at the end are transfers selected based upon the given time period. Is there a way to avoid selecting all the transfers ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 8:01 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
of course - make your transfers set (or bag) lazy.
add lazy="true" attribute and Hibernate will not load the set right after loading parent object


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