1 Reply Latest reply on Jul 2, 2007 3:27 PM by jfrankman

    Mapping @Enumerated to Numeric Values

    jfrankman Novice

      All,

      I have a column in a Table with values 1-9. Each value is a code that represents a certain value i.e.

      1=Annual
      2=Semi Annual
      3=Quarterly

      I want to map this column to an Enumeration. I have created the following Enumeration:



      @Name("typePayment")
      public enum TypePayment
      {
      1("Annual"),
      2("SemiAnnual"),
      3("Quarterly")
      }
      


      In my class I have mapped a column to this enumeration as follows:

       @Column(name = "TYPEPAYMENT")
       @Enumerated(EnumType.STRING)
       public TypePayment getTypePayment() {
       return typePayment;
       }
      


      The problem is any value beginning with a number is not allowed as an enumerated value. So I get a compile error in my enumeration. I can change the enumeration to contain something like A, B, C instead of 1,2,3 but I am working against legacy data and cannot easily change the data values contained in the table from numbers to letters.

      So my question is, how can I map an enumeration to a table column where the values contained in the column will be digits 0-9?


        • 1. Re: Mapping @Enumerated to Numeric Values
          jfrankman Novice

          I found a partial solution. If I do the following:

          1.change the column from varchar to integer in the DB table.
          2. Change the EnumType to ORDINAL

          @Column(name = "TYPEPAYMENT")
           @Enumerated(EnumType.ORDINAL)
           public TypePayment getTypePayment() {
           return typePayment;
           }


          Then I can get it to work. This works except there is a gap in the values. Allowed values are 1-7 and 9. Zero and Eight are excluded. But since the ordinal begins with zero there is not a way for me to handle this unless I put a dummy enumerated values in positions 0 and 8 in my enumeration.

          public enum TypePayment
          {
          DUMMY0("dummy0"),
          ANNUAL("Annual"),
          SEMI("Semi-Annual ($4 Fee)"),
          FOUR_PAY("4-Pay ($12 Fee)"),
          QUARTERLY("Quarterly ($12 Fee)"),
          TWELVE_PAY("12-Pay (No Fee)"),
          MONTHLY("Monthly"),
          DEFERRED("Deferred ($7 Fee)"),
          DUMMY8("dummy8"),
          OTHER("Other")
          ;
          


          The problem with the above solution is that I will have to exclude ordinals 0 and 8 in my application by makeing them not appear in drop down boxes, etc. I wish there was a way I could map and enumeration to numeric values using the EnumType(STRING).

          Any ideas are appreciated.