How to use enums with JPA

前端 未结 11 1071
梦如初夏
梦如初夏 2020-12-03 02:47

I have an existing database of a film rental system. Each film has a has a rating attribute. In SQL they used a constraint to limit the allowed values of this attribute.

相关标签:
11条回答
  • 2020-12-03 03:01

    Resolved!!! Where I found the answer: http://programming.itags.org/development-tools/65254/

    Briefly, the convertion looks for the name of enum, not the value of attribute 'rating'. In your case: If you have in the db values "NC-17", you need to have in your enum:

    enum Rating {
    (...)
    NC-17 ( "NC-17" );
    (...)

    0 讨论(0)
  • 2020-12-03 03:02

    The problem is, I think, that JPA was never incepted with the idea in mind that we could have a complex preexisting Schema already in place.

    I think there are two main shortcomings resulting from this, specific to Enum:

    1. The limitation of using name() and ordinal(). Why not just mark a getter with @Id, the way we do with @Entity?
    2. Enum's have usually representation in the database to allow association with all sorts of metadata, including a proper name, a descriptive name, maybe something with localization etc. We need the easy of use of an Enum combined with the flexibility of an Entity.

    Help my cause and vote on JPA_SPEC-47

    0 讨论(0)
  • 2020-12-03 03:02

    use this annotation

    @Column(columnDefinition="ENUM('User', 'Admin')")
    
    0 讨论(0)
  • 2020-12-03 03:03

    What about this

    public String getRating{  
       return rating.toString();
    }
    
    pubic void setRating(String rating){  
       //parse rating string to rating enum
       //JPA will use this getter to set the values when getting data from DB   
    }  
    
    @Transient  
    public Rating getRatingValue(){  
       return rating;
    }
    
    @Transient  
    public Rating setRatingValue(Rating rating){  
       this.rating = rating;
    }
    

    with this you use the ratings as String both on your DB and entity, but use the enum for everything else.

    0 讨论(0)
  • 2020-12-03 03:05

    have you tried to store the ordinal value. Store the string value works fine if you don't have an associated String to the value:

    @Enumerated(EnumType.ORDINAL)
    
    0 讨论(0)
  • 2020-12-03 03:07

    Enum public enum ParentalControlLevelsEnum { U("U"), PG("PG"), _12("12"), _15("15"), _18("18");

    private final String value;
    
    ParentalControlLevelsEnum(final String value) {
        this.value = value;
    }
    
    public String getValue() {
        return value;
    }
    
    public static ParentalControlLevelsEnum fromString(final String value) {
        for (ParentalControlLevelsEnum level : ParentalControlLevelsEnum.values()) {
            if (level.getValue().equalsIgnoreCase(value)) {
                return level;
            }
        }
        return null;
    }
    

    }

    compare -> Enum

    public class RatingComparator implements Comparator {

    public int compare(final ParentalControlLevelsEnum o1, final ParentalControlLevelsEnum o2) {
        if (o1.ordinal() < o2.ordinal()) {
            return -1;
        } else {
            return 1;
        }
    }
    

    }

    0 讨论(0)
提交回复
热议问题