3

Overview

I try to use postgres custom enums with Hibernate running on WildFly10 (also tested with WildFly9 with same result). While searching I found the solution which was mentioned at several points, e.g.

So basically I added a custom UserType and added a @Type annotation pointing to the specific java type / converter. It works, I can use it in a JEE app and also in a JSE app without any problem.

Problem / Question

If I enable validation

<property name="hibernate.hbm2ddl.auto" value="validate"/> 

I get the following error

Schema-validation: wrong column type encountered
in column [gender] in table [test]; 
found [gender_type (Types#VARCHAR)], but expecting [uuid (Types#OTHER)]

Why expecting UUID when gender_type is correct?

Details

The PostgreSQL type:

CREATE TYPE gender_type as ENUM ('MALE','FEMALE');

The PostgreSQL table

create table test (
  name varchar(200) NOT NULL PRIMARY KEY,
  gender gender_type NOT NULL
);

The Java enum

public enum GenderEnum {

  MALE ("MALE"),
  FEMAIL ("FEMALE");

  private String value;

  private GenderEnum(String value) {
    this.value = value;
  }

  public String getValue() {
    return value;
  }        
}

The Java EnumType

public class GenderEnumType extends GenericEnumType<String, GenderEnum> {

  public GenderEnumType() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    super(GenderEnum.class, GenderEnum.values(), "getValue", Types.OTHER);
  }


  @Override
  public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
    return nullSafeGet(rs, names, owner);
  }

  @Override
  public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
      nullSafeSet(st, value, index);
    }        
  }

The GenericEnumType is basically the one from http://octagen.at/2014/10/postgresql-custom-data-types-enum-in-hibernate/.

Test entity: neither

@Basic(optional = false)            
@Enumerated (EnumType.STRING)
@Type (type = "full.path.to.GenderEnumType")
@Column(name = "gender")
private GenderEnum gender;

nor

@Basic(optional = false)               
@Type (type = "full.path.to.GenderEnumType")
@Column(name = "gender")
private GenderEnum gender;

works.

Community
  • 1
  • 1
Joern
  • 193
  • 8
  • 1
    I am not sure but have you tried it without the `@Type` annotation. I can imagine that hibernate gets confused with `@Type` and `@Enumerated(EnumType.STRING)`. Just try it with the `@Enumerated` only. – Gregor Valentin Feb 15 '16 at 14:58
  • Relevant related question: http://stackoverflow.com/questions/27804069/hibernate-mapping-between-postgresql-enum-and-java-enum – Gimby Feb 15 '16 at 15:32
  • if I use only @Enumerated it does not work at all. Hibernate is then writing the data as 'MALE' into the database so postgres is complaining on wrong type for column gender (expected is 'MALE'::gender_type). As written above, the application is fully working, I can read and insert into the database using GenderEnum on the java side - it only fails on validation – Joern Feb 16 '16 at 11:23
  • I think my solution is basically what also Kenny described in the above related question. @Kenny: were you able to support validation? – Joern Feb 17 '16 at 10:25

0 Answers0