-->
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: Count and results query combined
PostPosted: Sat Aug 30, 2008 6:30 am 
Newbie

Joined: Mon Jul 31, 2006 6:28 am
Posts: 2
Hi

I am trying to use Hibernate to create the following queries using Criteria and HQL. I had been able to do this via Critieria and Projections e.g. obtianing results and resetting Projection and reusing the Critieria to get the rowcount(). However looking at the generated sql this causes Hibernate to fire off 2 queries, which is not what we want.

Another solution has been to add a formula to a count field (which is transient) to get the count of the resultset.

I have been trying to do this in HQL, I have had some success, however but running into problems transforming the transient count field.

here are 2 queries that I have been playing around with and produce the same result:

1:
select v.*,count(*) over (partition by v.id) as count from myview v where v.id = upper('user');

2:
select v.*, (select count(*) from myview v1) from myview v WHERE id = upper('user1')

My Domain Object has a getter/setter for Count annotated Transient.

And here is the code which is fine without trying to bring the count field, when including count(*), I cannot get it to transform to the MyView.class.

I have been using ResultTransformer but having problems with it (not shown below) e.g. invalid column or object not castable to MyView:

Code:
String query = "select {v.*},count(*) over "+
         "(partition by v.id) as count from myview v where v.id= :id";
         
         Query q = session.createSQLQuery(query).addEntity("v",MyView.class);
         //.addScalar("count", Hibernate.INTEGER);
         
         q.setParameter("id", new String("user1"));
         q.list();
         
         System.out.println("q.list() :"+q.list().size());
         System.out.println("q.list() :"+q.list());
         
         for (Iterator iterator = q.list().iterator(); iterator.hasNext();) {
            MyView view = (MyView) iterator.next();
            System.out.println("id:"+view.getId());
         }


Any advice appreciated.

Thanks

Nick

Code:
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2008 6:46 am 
Newbie

Joined: Wed Jul 09, 2008 11:31 am
Posts: 2
Try something like
Try to print class of items it returned:
Code:
         List<Object> somethings = q.list();
         Object something = somethings.get(0); //assuming it returns at least one row
         System.out.println(something.getClass());


My guess is it returns Object[] so this should work:
Code:
         List<Object[]> tmp = q.list();
                  for (Object[] oa:tmp) {
            MyView view = (MyView) oa[0];
            Integer count = (Integer) oa[1];
            System.out.println("id:"+view.getId());
            System.out.println("count:"+count);
         }


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.