public class FooConverter implements AttributeConverter<Foo, String> {
private static Map<Foo, String> typeMapping;
static {
typeMapping = new EnumMap<>(Foo.class);
typeMapping.put(Foo.BAR_1, "baa_1");
typeMapping.put(Foo.BAR_2, "baa_2");
}
@Override
public String convertToDatabaseColumn(Foo t) {
return typeMapping.get(t);
}
@Override
public Foo convertToEntityAttribute(String s) {
for (Map.Entry<typeMapping, String> entry : typeMapping.entrySet()) {
if (entry.getValue().equalsIgnoreCase(s)) {
return entry.getKey();
}
}
throw new IllegalArgumentException("Invalid value for enum: " + s);
}
}