I have an entity with an element of type enum:
@Column(name = "COL_NAME")
@Convert(converter = EnumConverter.class)
private COLNAME colname;
I need a generic converter (I don't want to write one new converter for each enum I'll have in my entities)
import java.lang.reflect.*;
@Converter(autoApply = false)
public class EnumConverter implements AttributeConverter<Object, String>{
@Override
public String convertToDatabaseColumn(Object attribute) {
String valuetoconvert = attribute.toString();
//do something on valuetoconvert
return valueconverted;
}
@Override
public Object convertToEntityAttribute(String dbData) {
// Object to return with dbData read from DB and modified
return objectconverted
}
}
In convertToEntityAttribute I try with Enumeration.valueOf, but this method need the class of enum. How can I try to find this?... if this is the correct way to do so. Thanks
P.S. I find, googling, some approach tending to minimize the code written, but in every case I must write one class for each enum. And I don't want this. Is it possible?