Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not extract ParameterizedType representation of AttributeConverter definition

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;
}
like image 702
Lukas Eichler Avatar asked Oct 27 '13 01:10

Lukas Eichler


1 Answers

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

like image 117
Mileta Cekovic Avatar answered Nov 03 '22 05:11

Mileta Cekovic