I'm working with an older codebase which has a LOT of entities using enums like this (currently using Hibernate 3), with a "generifying" GenericEnumUserType class:
@Entity
@Table(name = "dog")
@TypeDefs({
@TypeDef(name = "DogEnumType",
typeClass = com.GenericEnumUserType.class,
parameters = {
@Parameter(name = "enumClass", value = "com.DogStateEnum"),
@Parameter(name = "identifierMethod", value = "value"),
// could return different type depending on enum
@Parameter(name = "valueOfMethod", value = "fromValue")
}
)
})
public class Dog implements Serializable {
...
@Type(type = "DogEnumType")
public DogStateEnum getDogState() {
return this.dogState;
}
...
GenericEnumUserType (idea being valueOf method's java return type is mapped to the appropriate sql type):
public class GenericEnumUserType implements UserType, ParameterizedType {
...
private Method valueOfMethod;
private int[] sqlTypes;
...
public void setParameterValues(Properties parameters) {
// Something along the lines of:
String enumClassName = parameters.getProperty("enumClass");
enumClass = Class.forName(enumClassName).asSubclass(Enum.class);
String identifierMethodName = parameters.getProperty("identifierMethod");
identifierMethod = enumClass.getMethod(identifierMethodName);
identifierType = identifierMethod.getReturnType();
// TypeResolver is now deprecated!!
type = (AbstractSingleColumnStandardBasicType<? extends Object>) new TypeResolver().heuristicType(identifierType.getName(), parameters);
sqlTypes = new int[] { ((AbstractSingleColumnStandardBasicType<?>) type).sqlType() };
String valueOfMethodName = parameters.getProperty("valueOfMethod");
valueOfMethod = enumClass.getMethod(valueOfMethodName, new Class[] { identifierType });
}
...
Now that TypeResolver has been deprecated with no replacement, what is the appropriate way to look up the mapping from java type to sql type? And if this is impossible, what is the recommended approach to migrate this type of code?
I can see Hibernate now has org.hibernate.type.EnumType but the implementation assumes ordinal/string which is likely fine but is a much bigger refactor (there is also more than 1 generic enum).