Hibernate version: 3.2.0cr2
Hi all,
I'm trying to use Hibernate criteria api for getting query results based on the class type. I'm using table-per-subclass inheritance strategy.
If I get the results using the HQL, it works. The query looks like this:
select t from MyBaseClass t where t.class=MyChildClass
However, it doesn't work if I try to accomplish the same thing using criteria api with the following code:
Code:
Criteria criteria = session.createCriteria(MyBaseClass.class);
criteria.add(Restrictions.eq("class", MyChildClass.class));
criteria.list();
When I execute the above code I get ClassCastException:
Code:
Caused by: java.lang.ClassCastException: java.lang.Class
at org.hibernate.type.IntegerType.set(IntegerType.java:41)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:83)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:65)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:157
at org.hibernate.loader.Loader.doQuery(Loader.java:661)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollection
at org.hibernate.loader.Loader.doList(Loader.java:2145)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1562)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
Am I using the Criteria API correctly? Because there is no automatic conversion of the criteria value to the appropriate discriminator alias.
Any idea would be greatly appreciated.
Thank you.