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:
Code:
public FruitStandInventory (int quantityField, FruitEnumUserType fruitField);
So if the enum I'm using is something like
Code:
public enum FruitEnum { APPLE, ORANGE, PEAR }
and if I want to instantiate an object with values such as {24, FruitEnum.APPLE} then I can't just use the following code:
Code:
FruitStandInventory fruitStandInventory = new FruitStandInventory (24, FruitEnum.APPLE);
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:
Code:
public void setFruit (FruitEnumUserType fruit) { this.fruit = fruit; }
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
Code:
public void setFruit (FruitEnum fruit) { this.fruit = fruit; }
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.
--James
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