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.IntegerHow to solve it ?
thanks a lot.