-->
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.  [ 6 posts ] 
Author Message
 Post subject: Restricting collection results in query
PostPosted: Thu Dec 18, 2008 5:06 pm 
Newbie

Joined: Mon Mar 17, 2008 3:51 pm
Posts: 8
Location: Colorado Springs, CO
I'm using Hibernate 3.2 and have an object model where a class called Track has many time-ordered TrackPoints in a one-to-many relationship. I would like to build a HQL query that would return me a Track object, but with only a subset of its associated TrackPoints based off a Calendar value. I looked through Chapter 14 of Java Persistence with Hibernate, but cannot seem to find a place that addresses queries of this type. Any help would be greatly appreciated.

Here's a pasting of the relevant hbm files:

Code:
<hibernate-mapping>
   <class name="Track" table="track">
      <id name="id" column="id">
         <generator class="assigned"/>
      </id>
      <list name="trackPoints" cascade="all" lazy="false">
         <key column="track_id" not-null="true"/>
         <list-index column="point_position"/>
         <one-to-many class="TrackPoint"/>
      </list>
   </class>
</hibernate-mapping>

<hibernate-mapping>
   <class name="TrackPoint" table="track_point">
      <id name="id" column="id">
         <generator class="assigned"/>
      </id>
      <property name="DTG" type="calendar"/>
      <property name="x"/>
      <property name="y"/>
      <property name="z"/>
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 5:38 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It may be possible to do with a filter. Something like:

Code:
<list name="trackPoints" ....>
<filter name="timeFilter" condition="point_position between :start and :end"/>
....
</list>


You'll also need a <filter-def> tag. Read more about it here:
http://www.hibernate.org/hib_docs/v3/re ... te-filters


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 7:29 pm 
Newbie

Joined: Mon Mar 17, 2008 3:51 pm
Posts: 8
Location: Colorado Springs, CO
Thanks for the reply. I've tried it and can get it to filter track points when they are queried directly, but not when querying the parent Track object. I'll keep on trying and see if I can't get it to work.

Also, is this a pre-query or post-query filter? Meaning is this applied to the query being sent to the database or is it applied to the result set returned from the DB?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 9:26 pm 
Newbie

Joined: Mon May 14, 2007 1:57 am
Posts: 12
I'm living the same situation :'(
Is there any other way to restrict the elements to be retrieved from a left join fetch???
My scenario is about Artists and Albums (an artists have many albums). When I request an artist I need to fetch active artists and his "active" albums (active albums are marked by a true value)

Code:
Query q = getSession()
   .createQuery("select a from Artist as a " +
   "left join fetch a.albums as albs " +
   "where a.id=:aid and a.active=true "+
   "order by albs.titulo asc ")
   .setParameter("aid", artistId);


Any ideas???


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 10:10 pm 
Newbie

Joined: Fri Jun 06, 2008 2:03 pm
Posts: 9
Enyel wrote:
I'm living the same situation :'(
Is there any other way to restrict the elements to be retrieved from a left join fetch???
My scenario is about Artists and Albums (an artists have many albums). When I request an artist I need to fetch active artists and his "active" albums (active albums are marked by a true value)

Code:
Query q = getSession()
   .createQuery("select a from Artist as a " +
   "left join fetch a.albums as albs " +
   "where a.id=:aid and a.active=true "+
   "order by albs.titulo asc ")
   .setParameter("aid", artistId);


Any ideas???


I have not done much work on this and you may know this already, but have you looked at this?

http://www.hibernate.org/hib_docs/v3/ap ... esults(int)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2008 11:38 am 
Newbie

Joined: Mon Mar 17, 2008 3:51 pm
Posts: 8
Location: Colorado Springs, CO
Ok. I figured out why filtering on sets were not working. It had nothing to do with the filter or filter-def, instead it was because the data was being cached by the session so it didn't make sense to filter. If I either closed the session after the commit and opened a fresh one or called session.flush() after the transaction was committed then it works fine. Thanks for the help nordborg. For future reference, here's a link I found that addressed my problem.
http://forum.hibernate.org/viewtopic.php?t=938613


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