-->
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: How to sort the elements within the set of a fetched object?
PostPosted: Tue May 18, 2010 6:54 am 
Newbie

Joined: Tue May 18, 2010 6:16 am
Posts: 5
Good Morning, Comrades,

I have got a criteria that fetches a unique result (StaffShortInbox) inside which there is a set of posts (Post). I am trying to order the set inside the unique result by one of the properties (order by Post.posted). Consider the following classes:

StaffShortInbox
Fields:
Long staffId;
String firstName;
String surname;
Set posts = new HashSet(0); //contains a set of Posts VISIBLE by a staff member

Post
Fields:
Long postId;
StaffShort staff; //contains the OWNER of the post
String post;
Date posted; //the field the elements within the set to be ordered by

If I use the following fetching code, the "could not resolve property: posts.posted of: snowhouse.StaffShortInbox" error occurs. This is probably caused by the posts.posted hierarchy relation in ".addOrder(Order.desc("posts.posted"))" line.

StaffShortInbox inbox = (StaffShortInbox) connection.createCriteria(StaffShortInbox.class)
.add(Expression.eq("staffId", staffId))
.setFetchMode("posts", FetchMode.JOIN)
.setFetchMode("posts.staff", FetchMode.JOIN)
.addOrder(Order.desc("posts.posted"))
.uniqueResult();


If I use the following code, the JOIN fetching fails causing the "failed to lazily initialize a collection of role: snowhouse.StaffShortInbox.posts" error as I HAVE TO close the session before the unique result is returned.

StaffShortInbox inbox = (StaffShortInbox) connection.createCriteria(StaffShortInbox.class)
.add(Expression.eq("staffId", staffId))
.setFetchMode("posts", FetchMode.JOIN)
.setFetchMode("posts.staff", FetchMode.JOIN)
.createCriteria("posts")
.addOrder(Order.desc("posted"))
.uniqueResult();

What are my options in this situation?

M.


Last edited by MountainBeast on Tue May 18, 2010 8:48 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Criteria Order
PostPosted: Tue May 18, 2010 8:24 am 
Newbie

Joined: Tue May 18, 2010 6:16 am
Posts: 5
Or just to make the question simpler:
How to sort the elements within the set of a fetched object?

M.


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.