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.