-->
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: Paginating a Property Set
PostPosted: Mon Jul 02, 2007 10:12 am 
Newbie

Joined: Mon Feb 12, 2007 2:31 pm
Posts: 4
Hibernate version: Hibernate 3 with Spring Support

Mapping documents:
<class name="MailingList" table="list">
<id name="id" column="id" type="integer">
<generator class="identity" />
</id>

<set name="recipients" table="recipient_list">
<key column="list_id"/>
<many-to-many class="Recipient" column="recipient_id"/>
</set>
</class>


Name and version of the database you are using: MySQL 4.0.21

Hello,

I'm attempting to use the Criteria API to paginate through a set that is within a MailingList object. I would like to be able to return paginated results of the underlying set within a certain mailing list (I would be able to constrain the query for the list id). I have tried to use projection to return the recipients set and then paginate, but it returns the whole MailingList object, not the underlying Recipient objects in order to paginate.

I also tried to use the join table (recipient_list in this case) to filter a criteria query on the Recipient class, using the data in the join table to filter out all recipients of a given list. I never had success with this.

Any suggestions?

Basically, I'd like to be able to do something like this:
Code:
session.createCriteria(MailingList.class).add(Expression.eq("id",listId")).setProjection(Projections.property("recipients")).setFirstResult(0).setMaxResults(10).list()


or

Code:
session.createCriteria(Recipient.class).createCriteria(MailingList.class).add(Expression.eq("id",listId")).setFirstResult(0).setMaxResults(10).list()


I think the first method is closer, but I'm not that familiar with using complicated joins with the newer projection classes.


Top
 Profile  
 
 Post subject: Paginating a Property Set Figured Out
PostPosted: Tue Jul 03, 2007 10:18 am 
Newbie

Joined: Mon Feb 12, 2007 2:31 pm
Posts: 4
Hello All,

I figured out an answer to my question from yesterday and I figured I would share with you all.

My problem boiled down to having a criteria query return the objects of a set within an object so I could do pagination on these objects.

I found this page: http://blog.hibernate.org/cgi-bin/blosx ... ql_and_sql

Which pointed me to Result Transformers. I wanted to try to get the query to return the members of the underlying set rather than the root entity of my query. I found the PassThroughResultTransformer which returned a mapping of the objects that I wanted with their root entities. After downloading the source for this class and modifying it, I came up with something that worked.

My Criteria Query:
Code:
Criteria c=sessionFactory.getCurrentSession().createCriteria(MailingList.class)
            .add(Expression.eq("id", listId))
            .add(Expression.eq("siteId", siteId))
            .createCriteria("recipients")
            .setResultTransformer(new RecipientResultTransformer())
            .setMaxResults(10).setFirstResult(10*page.intValue());


RecipientResultTransformer:
Code:
public class RecipientResultTransformer implements ResultTransformer {

   public List transformList(List collection) {
      return collection;
   }

   public Object transformTuple(Object[] tuple, String[] aliases) {
      return tuple[0]; //Gets the lowest level criteria of the underlying result
   }

}


If anyone from the Hibernate Team could suggest improvements, or ways which I was lucky to get it work, please do so, I'd like to try to reuse this technique in other places of my application.


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.