4 Replies Latest reply on Feb 21, 2007 11:40 AM by onormann

    Using enums with entity beans

    dsouza

      Hi,

      I've been playing around with java 5 type-safe enums and persistence works neatly with them. But I have a question.

      Take for example this very simple enum:

      public enum MyEnum implements Serializable {
       HIGH,
       LOW
      }
      


      If I create an entity bean that has MyEnum as one of it's fields, it will persist HIGH as 0 and LOW as 1 in the database since they are the respective ordinals for each item in the enum.

      Now let's say I need to change this enum to:

      public enum MyEnum implements Serializable {
       HIGH,
       MEDIUM,
       LOW
      }
      


      This breaks my implementation since now LOW = 2. Of course I could just place the MEDIUM after LOW, but this is just an example of what I would need to do. In order to maintain the order inside my code (specially on larger enums) It would be great if I could just add to my enum anywhere I wanted to.

      So I thought I could do something such as:

      public enum MyEnum implements Serializable {
       HIGH(1000),
       LOW(100)
      
      ...
      }
      


      By doing this I would have to have a constructor for the enum and a method to retrieve the value I'm passing. Now I could easily add a MEDIUM(500) right in the middle.
      But the persistence layer would still be calling ordinal() to obtain an ordinal value to persist in the database. I can't override the ordinal() method because it's final. Is there any way I can get the container to call a different method to obtain an ordinal value for each enum? Any other ideas on how this could be solved?



        • 1. Re: Using enums with entity beans
          epbernard

          you can create an Hibernate UserType and use @Type

          • 2. Re: Using enums with entity beans
            dsouza

            Exactly what I was looking for! Thanks!

            However it is the first time I use @Type and I'm having some problems.

            As in the docs, I created a UserType class, implemented all methods:

            public class EnumUserType implements UserType {
            
            public Class returnedClass() {
             return ConditionType.class;
            }
            
            public int[] sqlTypes() {
             return new int[] { Types.NUMERIC };
            }
            ...
            


            and made the appropriate changes in my entity bean:

            @Type(type = "mypackage.EnumUserType")
            @Column(name = "COND_TYPE", nullable = false)
            public ConditionType getConditionType() {
            ...
            


            ConditionType is an enum as I described in my previous post.

            now I'm getting this when I deploy:

            
            org.hibernate.MappingException: Could not determine type for: mypackage.EnumUserType, for columns: [org.hibernate.mapping.Column(COND_TYPE)]
             at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
             at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
            ...
            


            Still can't figure out what I'm doing wrong. Any ideas on what might be causing this?


            • 3. Re: Using enums with entity beans
              dsouza

              Never mind that last post... It was a typo in my package's name.

              • 4. Re: Using enums with entity beans
                onormann

                You also can use the @Enumerated - annotation and choose the type ORDINAL or STRING, so you won't get problems with the order of newly added enum-constants.


                @Enumerated(EnumType.STRING)
                @Column(name="CATEGORY", nullable=false)
                private Category category;