我想使用新的JPA 2.1功能来创建自定义通用枚举转换器。但在部署时,我收到此错误:Caused by: org.hibernate.AssertionFailure: Could not extract ParameterizedType representation of AttributeConverter definition from AttributeConverter implementation class [de.lukaseichler.pomodoro.task.entity.converter.PriorityConverter]
我在Wildfly 8.0.0.Beta1上使用Hibernate 4.3.0和JPA 2.1。 我在做什么,或者这是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;
}
答案 0 :(得分:6)
作为一种解决方法,您可以这样做:
@Converter(autoApply = true)
public class PriorityConverter extends EnumConverter<Priority>
implements AttributeConverter<Priority, String> {}
此Hibernate错误报告在:https://hibernate.atlassian.net/browse/HHH-8854