-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem ordering resultSet by Criteria
PostPosted: Wed Mar 28, 2007 9:32 am 
Newbie

Joined: Wed Mar 28, 2007 9:00 am
Posts: 3
Hi!

We have the following problem. We have a class JobGroup with an One-To-Many relation to Jobs. So, the jobGroup has a Vector of jobs.

Now, we want to fetch the Jobgroups and jobs in one query, ordering by jobGroup-id and by job-id.
The code-snippet should make it more clear:

Code:
public Vector<JobGroup> getJobGroupsAndJobs() throws Exception {
        try {
            Session s = ((TransactionScopedEntityManager)manager).getHibernateSession();
            List<JobGroup> l = s.createCriteria(JobGroup.class)
                .setFetchMode("jobs", FetchMode.JOIN)
                .addOrder(Order.asc("id"))
                //.addOrder(Order.asc("jobs.id"))
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                .list();
           
            return new Vector<JobGroup>(l);
        } catch (HibernateException e) {
            ctx.setRollbackOnly();
            log.error("Error fetching jobgroup/jobs list", e);
            throw ResourceHelper.extractException(e);
        }       
    }


This works fine, when just ordering by jobgroup-id. But then, the jobs are in the wrong order. So, how can we add an OrderCriteria for the job-Id?? Isn't this possible? When using the uncommented line ( ".addOrder(Order.asc("jobs.id"))" ), we get an unknown proterty exception.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 28, 2007 9:47 am 
Newbie

Joined: Tue Sep 20, 2005 10:13 am
Posts: 5
try to use createAlias

Code:
List<JobGroup> l = s.createCriteria(JobGroup.class)
                .createAlias("jobs","j")
                .setFetchMode("jobs", FetchMode.JOIN)
                .addOrder(Order.asc("id"))
                .addOrder(Order.asc("j.id"))
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                .list();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 28, 2007 10:09 am 
Newbie

Joined: Wed Mar 28, 2007 9:00 am
Posts: 3
Hi!

You can't use aliases with fetchType.eager or fetchType.join! I read this in "Hibernate in Action". The said, one should use the criteria .resultMap() to use both. But there doesn't exist such a method!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 28, 2007 10:33 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Hmm...

What about:

Code:
List<JobGroup> l = s.createCriteria(JobGroup.class)
   .createAlias("jobs", "jobs", Criteria.LEFT_JOIN)
   .addOrder(Order.asc("id"))
   .addOrder(Order.asc("jobs.id"))
   .list();


Additionally, you will not need the ResultTransformer with the LEFT_JOIN. I am not sure if the jobs collection will be sorted properly, but it's worth a shot.


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