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();
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s