• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access the actual enum value of an Enum UserType?  RSS feed

James Adams
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to have a property of a Hibernate object map to an enumerated type, and I want to have this type represented in my database table as a VARCHAR. I have implemented a custom UserType class according to the example here: http://www.hibernate.org/265.html This appears to work as expected when I run SchemaExportTask, i.e. the columns are in fact created as type VARCHAR(255). However I can't figure out how I should go about instantiating the Hibernate objects so that they contain the correct enumerated value in this property, since the constructor and property setter methods take an object of the custom UserType as a parameter, rather than of one of the enum type's values.

For example, the constructor for the Hibernate object class that was generated by Hbm2JavaTask looks like this:

So if the enum I'm using is something like

and if I want to instantiate an object with values such as {24, FruitEnum.APPLE} then I can't just use the following code:

since the second argument to the constructor needs to be of the custom UserType and not the actual enum type that I need to use to set the value.

Also there doesn't appear to be a way to set the value using a property setter method, since it only allows you to set the custom UserType object and not the actual enumeration value being held by the custom UserType property. For example the property setter looks like this:

so there is no way to set the actual value represented by this field in the database. What I really need is a property setter such as

but this sort of thing doesn't look to be possible.

Can someone please explain what it is that I'm missing here? Essentially I need to know how to access the underlying enum that is being stored as the object's property, as there doesn't appear to be a way to get at it through the UserType interface.

Also will future versions of Hibernate be able to support simple enum types, as in the example above, without having to resort to creating a custom UserType?

Thanks in advance for any help/insight/suggestions.


Hibernate version: 3.0.5

Mapping documents:see above for example

Code between sessionFactory.openSession() and session.close():I'm using Spring's HibernateTemplate and HibernateDaoSupport classes

Name and version of the database you are using:MySQL
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!