I am mapping a Java 5 enum to an int column in the database.
I've got an EnhancedUserType based somewhat on the code here
http://www.hibernate.org/272.html
http://www.hibernate.org/312.html
How can I use this in an HQL query?
This is failing:
Code:
String hql = "from FoobarDTO "
+ "where projectId = :parentId "
+ "and alphaId = :alphaId "
+ "and kind = :kind and deleteStatus = 0";
Query query = HibernateUtil.getCurrentSession().createQuery(hql);
query.setString("alphaId", alphaId);
query.setInteger("parentId", parentId);
query.setEntity("kind", kind); // kind is a var of enum type Foobar.Kind
FoobarDTO dto = (FoobarDTO) query.uniqueResult();
Code:
<hibernate-mapping>
...
<property name="kind" type="com.mycompany.constants.Foobar$KindUserType">
<meta attribute="property-type">com.mycompany.constants.Foobar.Kind</meta>
</property>
...
Code:
org.hibernate.MappingException: Unknown entity: com.mycompany.constants.Foobar$Kind
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:550)
at org.hibernate.impl.SessionFactoryImpl.getIdentifierType(SessionFactoryImpl.java:637)
at org.hibernate.type.EntityType.getIdentifierType(EntityType.java:487)
at org.hibernate.type.EntityType.getIdentifierOrUniqueKeyType(EntityType.java:512)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:87)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:38)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:491)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1563)
at org.hibernate.loader.Loader.doQuery(Loader.java:673)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:811)
at com.mycompany.FoobarService.findByAlphaId(FoobarService.java:160)