-->
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: How to map sum(Boolean) to integer result ?
PostPosted: Thu Jun 11, 2009 3:23 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 3:46 am
Posts: 34
Location: Taiwan
Code:
        Criteria c = session.createCriteria(BlogDaily.class);
        c.add(Restrictions.eq("url", url));
        c.add(Restrictions.between("time", fromTime, toTime));
        ProjectionList plist = Projections.projectionList();
        plist.add(Projections.count("url"));
        plist.add(Projections.sum("frequent")); // "frequent" is Boolean type
       
        c.setProjection(plist);
        c.setCacheable(true);
        Object o = c.uniqueResult();
        Object[] oa = (Object[])o;
        System.err.println("oa[0] = " + oa[0]);
        int total = (Integer)oa[0];
        System.err.println("oa[1] = " + oa[1]);
        int freq =  (Integer)oa[1];


The generated SQL is :
Hibernate: select count(this_.url) as y0_, sum(this_.frequent) as y1_ from BlogDaily this_ where this_.url=? and this_.time between ? and ?

The problem is :
There are 2 projections , one is count of result rows , the other sum("frequent") is sum of boolean's 'true' type .
The query result in two integer columns , such as [797 , 30]
But hibernate treats second column (sum("frequent")) as Boolean ,
Maybe "frequent" is boolean column , so hibernate treats "sum of boolean" as boolean , too.
But that's not what I want.

And if I try to convert it to Integer , it throws :
java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.Integer

How to solve it ?
thanks a lot.


Top
 Profile  
 
 Post subject: Re: How to map sum(Boolean) to integer result ?
PostPosted: Fri Jun 12, 2009 5:54 pm 
Beginner
Beginner

Joined: Mon Jun 01, 2009 5:39 am
Posts: 34
I don't know how to do this directly, but typically, you would solve it in SQL as such:

Code:
select xx, sum(case frequent when true then 1 else 0 end) as thesum from thetable group by xx;


I know that HQL supports the case statement, but I don't know how exactly. Maybe the solutions lies here.


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.