1 Reply Latest reply on Dec 15, 2006 7:37 AM by alexg79

    @Enumerated with non-ordinal enumeration

    jeilong

      Given this enumeration:

      public enum Gender {
      
       MALE ("M"), FEMALE ("F");
      
       private String codeInDatabase;
      
       private Gender(String codeInDatabase) {
       this.codeInDatabase = codeInDatabase;
       }
      
       public String getCodeInDatabase() {
       return codeInDatabase;
       }
      }

      and this fragment from my POJO:
       @Enumerated(EnumType.STRING)
       @Column(name = "gender")
       public Gender getGender() {
       return gender;
       }
       public void setGender(Gender gender) {
       this.gender = gender;
       }


      In the database the column "gender" contains the value M or F. When I query the pojo I get the following exception:
      javax.ejb.EJBException: java.lang.IllegalArgumentException:
      Unknown name value for enum class com.company.enumeration.Gender: M


      Is this a bug? Or is using non-ordinal enumerations not possible at all? I'm using 4.0.5.GA with the EJB3 profile.

        • 1. Re: @Enumerated with non-ordinal enumeration
          alexg79

          You have misunderstood the way it works.
          What if you had multiple attributes, like MALE("M", 1, 2)? How would it work then, hmm??
          The answer is that it doesn't -- it uses the enumeration names, such as MALE and FEMALE. Replace "M" and "F" in your database with "MALE" and "FEMALE", respectively.