0

I have a legacy database table that, for simplicity sake looks like:

  table address{
    varchar line1
    varchar line2
    varchar line3
    varchar(1) deliveryline
  }

There is a check constraint on deliveryline guaranteeing it has the values '1,'2', or '3'.

This seems like a good candidate for enumeration in hibernate. I have an entity that looks like this representing the Address table:

  public class Address{
    String line1;
    String line2;
    String line3;
    DeliveryLine deliveryLine;
  }

I normally use @Enumerated(EnumType.STRING) on when mapping enums, but that strategy does not work here. For example:

public enum DeliveryLine { 1,2,3 } This does not compile since the valid values in the database (1,2,3) are not valid Java Identifiers.

Is there a straightforward way to coerce this mapping in hibernate?

BuffaloBuffalo
  • 7,703
  • 4
  • 28
  • 29

2 Answers2

4

Look at GenericEnumUserType described at hibernate.org (Under "Flexible solution")

If you're using Hibernate 4 you'll have to use a modified version as discussed here

Community
  • 1
  • 1
mtpettyp
  • 5,533
  • 1
  • 33
  • 33
  • I will be moving to Hibernate4 in the near future and it looks like I will be using your answer on the second link to make this work correctly. In the meantime I mapped `delivery_line` column to a String property and linked the getter/setter property to the enumerated property I wanted. Not exactly the cleanest solution but it works in the interim. – BuffaloBuffalo Feb 10 '12 at 17:24
1

Since you're locked into a varchar for column type, I think you're looking at a custom UserType. Such as: http://docs.jboss.org/hibernate/orm/4.0/manual/en-US/html/types.html#types-custom-ut

cscott530
  • 1,658
  • 16
  • 25