-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate Criteria with Subselect and Limit
PostPosted: Wed Jun 25, 2014 7:10 am 
Newbie

Joined: Wed Jun 25, 2014 7:01 am
Posts: 2
Hi all,
I have a complex sql query but the simplest form of it look likes below (I want to retrieve last 10 records' distribution based on status, e.g.:there are 4 inactive and 6 active users in last 10 records):

Code:
SELECT status, count(id) FROM
  (SELECT id, status FROM `User` ORDER BY id DESC LIMIT 10) t1
group by status order by status desc


How could you write this query in Criteria Api. Here what I've done, but the problem is, limit is not working in subquery.

Code:
DetachedCriteria subQuery = DetachedCriteria.forClass(User.class);

subQuery.getExecutableCriteria(getCurrentSession()).setMaxResults(10);

subQuery.setProjection(Projections.projectionList()
                  .add(Projections.property("id"))
                  .add(Projections.property("status")))
                  .addOrder(Order.desc("id"));

Criteria criteria = getCurrentSession().createCriteria(User.class);
criteria.setProjection(Projections.projectionList()
          .add(Projections.property("status"), "status")
               .add(Projections.count("id"), "count")
               .add(Projections.groupProperty("status")))
               .addOrder(Order.desc("status"))
               .setResultTransformer(Transformers.aliasToBean(UserViewBean.class));
      
criteria.add(Subqueries.exists(subQuery));


Any help will be appreciated.
Regards


Top
 Profile  
 
 Post subject: Re: Hibernate Criteria with Subselect and Limit
PostPosted: Sat Jun 28, 2014 12:23 pm 
Newbie

Joined: Wed Jun 25, 2014 7:01 am
Posts: 2
Any help ?


Top
 Profile  
 
 Post subject: Re: Hibernate Criteria with Subselect and Limit
PostPosted: Mon Jun 30, 2014 6:23 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi,
you cannot use subqueries in the FROM clause with hibernate.

Couldn't you write the query like:
Code:
SELECT status, count(id)
FROM `User`
WHERE id IN (SELECT id FROM `User` u2 ORDER BY u2.id DESC LIMIT 10)
GROUP BY status
ORDER BY status DESC


Code:
DetachedCriteria subQuery = DetachedCriteria.forClass(User.class, "u2");
subQuery.getExecutableCriteria(getCurrentSession()).setMaxResults(10);
subQuery.setProjection(Projections.projectionList()
                  .add(Projections.property("u2.id"))
                  .addOrder(Order.desc("u2.id"));

Criteria criteria = getCurrentSession().createCriteria(User.class);
criteria.setProjection(Projections.projectionList()
          .add(Projections.property("status"), "status")
               .add(Projections.count("id"), "count")
               .add(Projections.groupProperty("status")))
               .addOrder(Order.desc("status"))
               .setResultTransformer(Transformers.aliasToBean(UserViewBean.class));
criteria.add(Subqueires.propertyIn( "id", subQuery);


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