Hello hibernate'enthusiasts
I'm using hibernate 4.1.12 version, found a can't solve bug. The code is as follows.
Code:
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
Code:
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