Thursday, June 9, 2016

Use a specific value of enum for a column value in JPA

If you use JPA 2.1, you can use JPA converter as follows:

@Converter(autoApply = true)
public class RatingScoreConverter implements AttributeConverter<RatingScore, Integer> {

@Override
public Integer convertToDatabaseColumn(RatingScore attribute) {
return attribute.getScore();
}

@Override
public RatingScore convertToEntityAttribute(Integer dbData) {
return RatingScore.getValueByScore(dbData);
}

}

Reference:
https://dzone.com/articles/mapping-enums-done-right

No comments:

Post a Comment