jpa-spec
  1. jpa-spec
  2. JPA_SPEC-47

Usability of @Enumerated and @MapKeyEnumerated

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Labels:
      None

      Description

      In mapping a pre-existing schema, it is rarely possible to map a given set of constants using an @Enumerated:

      • Ordinals are non-sequential
      • Names are incompatible with Java Enum Constants
      • Names cannot be aliased to something more useful or conventional for Java Naming standards

      Limitation is because specification states the name() will be used of an enum.
      A typical example: Database uses 'N/A', which can never be mapped to an enum. Intuition will let us look for a solution to map this to 'NOT_AVAILABLE', both a valid name for an Enum Constant, and also conventional naming.

      On top of this, an enum often has a representation in the Database, and a constant has usually other additional attributes that could be useful.
      Better than using the name() of an @Enumerated, we should map the enumerated similar as an @Entity, and use the @ID.

        Activity

        jd3714att created issue -
        ldemichiel made changes -
        Field Original Value New Value
        Assignee ldemichiel [ ldemichiel ]
        Hide
        jd3714att added a comment - - edited

        Would this not be more elegant than using a @Converter to solve the problem.

        @Enumerated
        public enum Language {
          ENGLISH_US("en-US"),
          ENGLISH_BRITISH("en-BR"),
          FRENCH("fr"),
          FRENCH_CANADIAN("fr-CA");
          @ID
          private String code;
          @Column(name="DESCRIPTION")
          private String description;
        
          Language(String code) {
            this.code = code;
          }
          
          public String getCode() {
            return code;
          }
        
          public String getDescription() {
            return description;
          }
        }
        
        Show
        jd3714att added a comment - - edited Would this not be more elegant than using a @Converter to solve the problem. @Enumerated public enum Language { ENGLISH_US( "en-US" ), ENGLISH_BRITISH( "en-BR" ), FRENCH( "fr" ), FRENCH_CANADIAN( "fr-CA" ); @ID private String code; @Column(name= "DESCRIPTION" ) private String description; Language( String code) { this .code = code; } public String getCode() { return code; } public String getDescription() { return description; } }
        Hide
        HajoLemcke added a comment -

        This looks great. And it would solve the problem to have a separate converter for each enumeration because a generic converter can not be build type save.

        Show
        HajoLemcke added a comment - This looks great. And it would solve the problem to have a separate converter for each enumeration because a generic converter can not be build type save.

          People

          • Assignee:
            ldemichiel
            Reporter:
            jd3714att
          • Votes:
            4 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated: