java.lang.ClassCastException: org.hibernate.impl.CriteriaImpl cannot be cast to com.YourClass

When working on any application which Uses Java + Hibernate 3, you face errors like the one shown below:

java.lang.ClassCastException: org.hibernate.impl.CriteriaImpl cannot be cast to com.YourClass

The source code throwing error:

private static Lookup getLookupFromId (Session session, Long status) {
 
 return (Lookup) session.createCriteria(YourClass.class)
 .add(Restrictions.idEq(status));
}
Solution to resolve this error is quite Simple as below by adding .uniquResult():
private static Lookup getLookupFromId(Session session, Long status) {
 
 return (Lookup) session.createCriteria(YourClass.class)
 .add(Restrictions.idEq(status)).uniqueResult();
}

Join Discussion

This site uses Akismet to reduce spam. Learn how your comment data is processed.