On my project I have the task of persisting a complex data model involving over 260 enums in different classes.
All these enums have a common interface, and there is a utility class that can create the enum instance from the serialization value and class name.
It is not acceptable to use the JPA Enum persistence technique, because neither ordinal nor named make any sense.
When I do not annotate the property with any annotation, hibernate defaults to use the internal org.hibernate.type.EnumType for persistence and is apparently successful doing this. Though I am not sure about that.
Just like
HHH-7108
, which might be accidentally closed, for me the issue still persisted.
1) I cannot create a type for the concrete enum class, it is never chosen. 2) I cannot use the Integrator-Technology to add a configuration type override for the enum type, it is never chosen 3) If I use the @Type annotation, I can specify a type that has to be chosen and it works.
This solution has multiple effects
1) I must create different types for each of my enums, thus duplicating the code with 260 strange looking copy-paste classes 2) I can use the ParameterizedType interface, and put in parameters to each occurence of @Type in the properties, so that I only need writing one class, and using some parameter magic, I can access the returned type in setParameterValues and initialize the rest. This leads me creating some string constants to define returnedClasses of properties so that I can use them as a parameter, which looks ugly and strange
Then I discovered DynamicParameterizedType. My Idea was to create a @TypeDef for name=org.hibernate.type.EnumType to Replace the EnumType of hibernate with mine. That way my type would be chosen, when enums were encountered and correctly parameterized - I hoped.
In this case, things stop working under certain circumstances. If my type is solely implementing DynamicParameterizedType, some obscure caching mechanism regarding SimpleValues (or referencedComponent?) seem to "copy" the first dynamic parameter detected in some entity class to get copied to any other EnumType that is detected elsewhere.
So, if there is only 1 enum in my project, the technique of replacing EnumType with my EnumType works, because the dynamic parameterization is functional.
But starting from the 2nd Enum, it becomes non-functional, because the "typeParameters" of the SimpleValue instances get replaced at some stage. I could even debug them being correctly filled at first by the SimpleValueBinder, however later they were replaced.
The solution for this funny issue is even more strange.
If I re-introduce the @Type annotation AND give it some dummy parameters like parameters = @Parameter(name="foo", value="bar")
it suddenly works. If I dont use parameters, it does not work.
In this case, I even do not need replacing org.hibernate.type.EnumType, because hibernate is forcing me to use @Type everywhere; that means, I can also choose other names. However the problem of DynamicParameterizedType not being filled unless the @Type annotation contains senseless parameters still persists.
|