-->
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.  [ 1 post ] 
Author Message
 Post subject: detached criteria ordering projection column
PostPosted: Sun Mar 24, 2013 1:31 am 
Newbie

Joined: Sun Mar 24, 2013 12:56 am
Posts: 3
Hi,

I have four tables as shown below. I would like to query by joining all these 4 tables and retrieve the email, registered date, last visit date, no of visits, and no Of times feedback provided and no of files uploaded.
Code:
Class User{
     int userId;
     String email;
     Date registeredDate;
     UserVisits userVisits; //one-to-one relation
     Set<UserFeedback> feedbacks; //one-to-many
     Set<UserUploads> uploads // one-to-many
}

Class UserVisits{
     int userId;
     Date lastVisited;
     int noOfVisits;
}

Class UserFeedback{
     int feedbackId;
     int userId;
     String feedback;
     Date feedbackDate
}

Class UserUploads{
      int fileId;
      User user;
      int fileName;
}


SQL Looks like

Code:
select this_.email as email, this_.created_date as created_date, userlogins1_.last_login as last_login, userlogins1_.login_count as login_count, fb_count, upload_count from user_details this_ left outer join user_logins userlogins1_ on this_.user_id=userlogins1_.user_id left outer join (select count(feedback_id) as fb_count,user_id from user_feedback group by user_id) as uf on this_.user_id=uf.user_id left outer join (select count(uploadid) as upload_count,user_id from user_uploads group by user_id) as m on this_.user_id=m.user_id order by this_.email asc


For the subqueries used detached criteria like
Code:
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(User.class, "user");
criteria.createAlias("user.userLogins","userLogins",Criteria.LEFT_JOIN);

DetachedCriteria fbCriteria = DetachedCriteria.forClass(UserFeedback.class, "feedback");
fbCriteria.add(Restrictions.eqProperty("feedback.userId", "user.userId"));
fbCriteria.setProjection(Projections.alias(Projections.count("feedback.userId"), "fbcount"));
fbCriteria.addOrder(Order.asc("fbcount"));
criteria.add(Subqueries.exists(fbCriteria));

DetachedCriteria uploadCriteria = DetachedCriteria.forClass(FileUploads.class, "uploads");
uploadCriteria.add(Restrictions.eqProperty("uploads.user.userId", "user.userId"));
uploadCriteria.setProjection(Projections.count("uploads.user.userId"));
criteria.add(Subqueries.exists(uploadCriteria));

List<User> users = criteria.list();


I expect that for the rows with "fbcount" "0", to appear first when ordered in ascending order and last when ordering in descending. but its not happening. The rows with "fbcount" value "0" are always placed at end.

Can one one please help me on this how to achieve.

Can any one please suggest me on how to achieve this.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.