I wanted to use the new JPA 2.1 Feature to create a custom generic Enum converter. But on deploy I get this error: Caused by: org.hibernate.AssertionFailure: Could not extract ParameterizedType representation of AttributeConverter definition from AttributeConverter implementation class [de.lukaseichler.pomodoro.task.entity.converter.PriorityConverter]
I am using Hibernate 4.3.0 on Wildfly 8.0.0.Beta1 with JPA 2.1. Am I doing something from or is this a error in hibernate?
Enumconverter.java
public abstract class EnumConverter<T extends Enum> implements AttributeConverter<T, String> {
private Class<T> type;
@Override
public String convertToDatabaseColumn(T attribute) {
return attribute.name();
}
@Override
public T convertToEntityAttribute(String name) {
if (type == null) {
getType();
}
return (T)Enum.valueOf(type, name);
}
private void getType() {
Class<?> converterClass = getClass();
while (true) {
Class<?> baseClass = converterClass.getSuperclass();
assert baseClass != null : "Converter must be derived from " + EnumConverter.class.getName();
if (baseClass == EnumConverter.class) {
break;
}
converterClass = baseClass;
}
Type genericSuperClass = converterClass.getGenericSuperclass();
assert genericSuperClass instanceof ParameterizedType : EnumConverter.class.getName() + "must be generic";
Type[] typeParms = ((ParameterizedType) genericSuperClass).getActualTypeArguments();
assert typeParms.length == 2 : EnumConverter.class.getName() + " must have 2 type parameters but has " + typeParms.length;
Type enumType = typeParms[1];
if(enumType instanceof ParameterizedType) {
enumType = ((ParameterizedType) enumType).getRawType();
}
assert enumType instanceof Enum<?> : "Entity must be a enum type";
type = (Class<T>) enumType;
}
}
PriorityConverter.java
@Converter(autoApply = true)
public class PriorityConverter extends EnumConverter<Priority>{
}
Priority.java
public enum Priority {
NONE, LOW, NORMAL, HIGH, TOP;
}
As a workaround you can do:
@Converter(autoApply = true)
public class PriorityConverter extends EnumConverter<Priority>
implements AttributeConverter<Priority, String> {}
This Hibernate bug is reported at: https://hibernate.atlassian.net/browse/HHH-8854
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With