views:

2171

answers:

2

We have a postgres DB with postgres enums. We are starting to build JPA into our application. We also have Java enums which mirror the postgres enums. Now the big question is how to get JPA to understand Java enums on one side and postgres enums on the other? The Java side should be fairly easy but I'm not sure how to do the postgres side.

+3  A: 

This involves making multiple mappings.

First, a Postgres enum is returned by the JDBC driver as an instance of type PGObject. The type property of this has the name of your postgres enum, and the value property its value. (The ordinal is not stored however, so technically it's not an enum anymore and possibly completely useless because of this)

Anyway, if you have a definition like this in Postgres:


CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');

Then the resultset will contain a PGObject with type "mood" and value "happy" for a column having this enum type and a row with the value 'happy'.

Next thing to do is writing some interceptor code that sits between the spot where JPA reads from the raw resultset and sets the value on your entity. E.g. suppose you had the following entity in Java:


public @Entity class Person {

  public static enum Mood {sad, ok, happy}

  @Id Long ID;
  Mood mood;

}

Unfortunately, JPA does not offer an easy interception point where you can do the conversion from PGObject to the Java enum Mood. Most JPA vendors however have some proprietary support for this. Hibernate for instance has the TypeDef and Type annotations for this (from Hibernate-annotations.jar).


@TypeDef(name="myEnumConverter", typeClass=MyEnumConverter.class)
public @Entity class Person {

  public static enum Mood {sad, ok, happy}

  @Id Long ID;
  @Type(type="myEnumConverter") Mood mood;

These allow you to supply an instance of UserType (from Hibernate-core.jar) that does the actual conversion:


public class MyEnumConverter implements UserType {

    private static final int[] SQL_TYPES = new int[]{Types.OTHER};

    public Object nullSafeGet(ResultSet arg0, String[] arg1, Object arg2) throws HibernateException, SQLException {

     Object pgObject = arg0.getObject(X); // X is the column containing the enum

     try {
      Method valueMethod = pgObject.getClass().getMethod("getValue");
      String value = (String)valueMethod.invoke(pgObject);   
      return Mood.valueOf(value);  
     }
     catch (Exception e) {
      e.printStackTrace();
     }

     return null;
    }

    public int[] sqlTypes() {  
     return SQL_TYPES;
    }

    // Rest of methods omitted

}

This is not a complete working solution, but just a quick pointer in hopefully the right direction.

arjan
>we are now moving our system to JPA too, but we have java enums mapped to varchar columns still had no problems with Hibernatevarchars look nice in the DB, but you miss 2 things compared to real enums in the DB: * Type safety, unless you want to add a check constraint to each and every column or use a FK to a separate table that holds all varchar values as a PK. * Speed. string lookups and compares are slower, even when indexed.So its a tradeoff anyway. Either you use varchars, and except the above restrictions, or you use native PG enums and accept the non-automatic mapping.
arjan
p.s.There is a third way, mapping by ordinal, but that's not really recommended; making the code hard to read a brittle; if you or remove a single constant from your Java enum, all ordinals that are already in the DB may become invalid!
arjan
A: 

I filed a bug report with a patch included for Hibernate: HHH-5188

The patch works for me to read a PostgreSQL enum into a Java enum using JPA.

Stefan L