-->
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.  [ 1 post ] 
Author Message
 Post subject: hibernate-release-4.1.12.Final
PostPosted: Wed May 22, 2013 10:51 pm 
Newbie

Joined: Wed May 22, 2013 10:16 pm
Posts: 3
Hello hibernate'enthusiasts
I'm using hibernate 4.1.12 version, found a can't solve bug. The code is as follows.
public int getTotalPage(Class<?> classType, Map<String,Object> condition) {
Session session = this.getSessionFactory().getCurrentSession();
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(classType);
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
CriteriaImpl impl = (CriteriaImpl) criteria;
Projection projection = impl.getProjection();
//criteria.add(Restrictions.allEq(condition));
criteria.add(Restrictions.eq("type",new Integer(1)));
//Execute the query
int totalCount = ((Number) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
criteria.setProjection(projection);
if (projection == null) {
criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
}
return totalCount;
}
If the code written so no problem,But if I comment out the criteria. The add (Restrictions. Eq (" type ", new Integer (1))); Code, and cancel the criteria. The add (Restrictions. AllEq (condition)); Code comments.Will appear the following error
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
I used to try to traverse each key in the Map, the value, the inside of each pair of key and value. Changes to the criteria. The add (Restrictions. Eq (key, value));,The code is as follows
Iterator<Map.Entry<String, Object>> iter = condition.entrySet().iterator();
while(iter.hasNext()) {
Map.Entry<String, Object> entry = iter.next();
String key = entry.getKey();
Object value = entry.getValue();
criteria.add(Restrictions.eq(key,value));
}
But will still offer the above error.
Please help me to indicate it, I change how to solve this problem, is my code has a problem, or the hibernate version has a problem. If you have any solution, respond to, or send E-mail to me.
Thank you very much


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.