Dexter Barnes
Dexter Barnes

Reputation: 73

Hibernate Envers with custom type for property

Im currently having an issue setting up Hibernate Envers in my Springboot application. I have an enum in one of my models which has an enum type and in my PostgreSQL database the column is of type "Type".

the property:

@Enumerated(STRING)
@Type(type = "pgsql_enum")
private VehicleTypes vehicleType;

the model class for this property is annotated with:

@Entity(name = "cars")
@Audited
@TypeDef(name = "pgsql_enum", typeClass = PostgreSqlEnumType.class)

and the custom type "PostgreSqlEnumType" is:

public class PostgreSqlEnumType extends EnumType {
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
    if (value == null) {
        st.setNull(index, OTHER);
    } else {
        st.setObject(index, value.toString(), OTHER);
    }
}}

Unfortunately, when I build this i get the following error:

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:954)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:882)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:360)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:382)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:371)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:336)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 39 more
Caused by: org.hibernate.MappingException: Unable to instantiate custom type: <myPackage>.PostgreSqlEnumType
    at org.hibernate.type.TypeFactory.custom(TypeFactory.java:217)
    at org.hibernate.type.TypeFactory.custom(TypeFactory.java:203)
    at org.hibernate.type.TypeFactory.byClass(TypeFactory.java:117)
    at org.hibernate.type.TypeResolver.heuristicType(TypeResolver.java:112)
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:416)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:398)
    at org.hibernate.mapping.Property.isValid(Property.java:225)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:595)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:265)
    at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:443)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)
    ... 46 more
Caused by: java.lang.NullPointerException
    at org.hibernate.type.EnumType.getAnnotation(EnumType.java:134)
    at org.hibernate.type.EnumType.getEnumType(EnumType.java:125)
    at org.hibernate.type.EnumType.setParameterValues(EnumType.java:80)
    at org.hibernate.type.TypeFactory.injectParameters(TypeFactory.java:152)
    at org.hibernate.type.TypeFactory.custom(TypeFactory.java:213)
    ... 57 more

I can't seem to get hibernate to recognise this custom type. Before implementing Envers this has been compiling fine.

version:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-envers</artifactId>
  <version>5.1.0.Final</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-java8</artifactId>
  <version>5.1.0.Final</version>
</dependency>

PostgreSQL type definition:

CREATE TYPE customer_cars.vehicle_types AS ENUM
('CAR', 'PUP');

Sorry for all the dumps, any help will be greatly appreciated!

Thanks!

Upvotes: 2

Views: 1468

Answers (1)

Naros
Naros

Reputation: 21113

This problem was resolved with HHH-12304.

You need to update to a version that includes that fix in order to avoid your problem unfortunately. Since this was not backported to 5.1, you'll need to consider one of the latest 5.2.x releases that include that fix.

Upvotes: 1

Related Questions