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