Mapping enum to a table with hibernate annotation

Posted by Thierry-Dimitri Roy on Stack Overflow See other posts from Stack Overflow or by Thierry-Dimitri Roy
Published on 2009-04-09T20:01:53Z Indexed on 2010/04/22 9:33 UTC
Read the original article Hit count: 193

I have a table DEAL and a table DEAL_TYPE. I would like to map this code:

public class Deal {
   DealType type;
}

public enum DealType {
   BASE("Base"), EXTRA("Extra");
}

The problem is that the data already exist in the database. And I'm having a hard time mapping the classes to the database.

The database looks something like that:

   TABLE DEAL {
      Long id;
      Long typeId;
   }

   TABLE DEAL_TYPE {
       Long id;
       String text;
   }

I know I could use a simple @OneToMany relationship from deal to deal type, but I would prefer to use an enum. Is this possible?

I almost got it working by using a EnumType.ORDINAL type. But unfortunately, my IDs in my deal type table are not sequential, and do not start at 1.

Any suggestions?

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate