gpt4 book ai didi

java - 无法提取 AttributeConverter 定义的 ParameterizedType 表示

转载 作者:搜寻专家 更新时间:2023-10-31 19:55:44 30 4
gpt4 key购买 nike

我想使用新的 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]

我在带有 JPA 2.1 的 Wildfly 8.0.0.Beta1 上使用 Hibernate 4.3.0。我是在做某事还是这是 hibernate 中的错误?

枚举转换器.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>{
}

优先级.java

public enum Priority {
NONE, LOW, NORMAL, HIGH, TOP;
}

最佳答案

作为解决方法,您可以:

    @Converter(autoApply = true)
public class PriorityConverter extends EnumConverter<Priority>
implements AttributeConverter<Priority, String> {}

此 Hibernate 错误报告于:https://hibernate.atlassian.net/browse/HHH-8854

关于java - 无法提取 AttributeConverter 定义的 ParameterizedType 表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19613765/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com