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.
- http://octagen.at/2014/10/postgresql-custom-data-types-enum-in-hibernate/
- Hibernate Enumeration Mapping
- java.lang.verifyError on hibernate specific usertype
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.