So I'm undertaking the task of converting our PersistentEnums to Apache's ValuedEnums (the Enums which have an integer), and making associated UserType.
Everything's going pretty good except for a fairly major snag... For thoes who are still a little lost I'll briefly outline the steps before I get to my problem:
First, for each PersistentEnum, extend Apache's ValuedEnums and use their rather nice interface to eliminate the need for the case statement (you also get hashCode and equals for free too).
Then, copy the EnumUserType from the example at the bottom of
http://www.hibernate.org/172.html into your project, except convert it to an int/Integer instead of a String.
Make an instance of this class for each Enumeration you want to persist with a no-args constructor which passes in the class type to the super().
For example: If you have an RGBEnum, make an RGBEnumType extends EnumUserType which passes in RGBEnum.class to the super() in the constructor:
Code:
public class RGBEnumType extends EnumUserType{
public RGBEnumType(){
super(RGBEnum.class);
}
}
Add a type='...' for each property in your mappings where you have a PersistentEnum, specifying the EnumUserType for that class.
Again for the RGBEnum example, if it was previously:
Code:
<property name='RGB' column='N_RGB' />
the new mapping would look like:
Code:
<property name='RGB' column='N_RGB' type='eg.example.RGBEnumType' />
Finally, remove PersistentEnum from the 'implements' in your enumerations.
You're now PersistentEnum free, and ready for H2.2
My problem:I have a query in which I'm doing a where, specifying one of the Enum's as the argument:
Code:
... and job.jobType = eg.example.JobTypeEnum.CAST
JobTypeEnum
used to be a PersistentEnum... JobTypeEnum.CAST is still valid, but now Hibernate has no clue how to deal with it (because it no longer implements the PersistentEnum interface).
When I try and do this query I get:
Code:
Could not format constant value to SQL literal: eg.example.JobType.CAST
I understand what's wrong here: Hibernate used to know how to deal with this; it would just call toInt() on it. Now it can't. What do I need to do to make this work again?
I absolutely understand the reasoning behind this change... but what can I do to keep the elegance afforded to me by PersistentEnum in my queries?
Any and all assistance is appreaciated.
-Travis Savo <tsavo@ifilm.com>