-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: HQL for querying a column represented by a Java 5 enum
PostPosted: Fri Nov 21, 2008 3:50 pm 
Newbie

Joined: Wed Oct 31, 2007 9:16 pm
Posts: 9
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)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 02, 2008 1:58 am 
Newbie

Joined: Wed Oct 31, 2007 9:16 pm
Posts: 9
Hmmm, no response. I figured out that the following works

Code:
Query query = HibernateUtil.getCurrentSession().createQuery(hql);
query.setString("alphaId", alphaId);
query.setInteger("parentId", parentId);
query.setInteger("kind", kind.getIntValue());

although I am stil hoping for a more elegant solution that
uses the enum directly rather than the int value. Is there
a way? I thought that the purpose of an EnhancedUserType
was to allow this.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.