Skip to content

Commit

Permalink
Envers throws MappingException: Type not supported for auditing #463
Browse files Browse the repository at this point in the history
  • Loading branch information
msdousti authored and vladmihalcea committed Aug 3, 2022
1 parent 43d76a0 commit e49879a
Show file tree
Hide file tree
Showing 20 changed files with 142 additions and 41 deletions.
Expand Up @@ -10,6 +10,7 @@
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.metamodel.relational.Size;
import org.hibernate.type.BasicType;
import org.hibernate.type.ForeignKeyDirection;
import org.hibernate.type.Type;
import org.hibernate.type.descriptor.java.IncomparableComparator;
Expand All @@ -31,7 +32,7 @@
*
* @author Vlad Mihalcea
*/
public abstract class ImmutableType<T> implements UserType, Type {
public abstract class ImmutableType<T> implements UserType, BasicType {

private final Configuration configuration;

Expand Down Expand Up @@ -338,4 +339,11 @@ public Object fromXMLNode(Node xml, Mapping factory) throws HibernateException {
Method valueOfMethod = ReflectionUtils.getMethodOrNull(clazz, "valueOf", String.class);
return valueOfMethod != null ? ReflectionUtils.invokeStaticMethod(valueOfMethod, xml.getText()) : null;
}

@Override
public String[] getRegistrationKeys() {
return new String[]{
getName()
};
}
}
Expand Up @@ -10,6 +10,7 @@
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.type.BasicType;
import org.hibernate.type.ForeignKeyDirection;
import org.hibernate.type.Type;
import org.hibernate.type.descriptor.java.IncomparableComparator;
Expand All @@ -31,7 +32,7 @@
*
* @author Vlad Mihalcea
*/
public abstract class ImmutableType<T> implements UserType, Type {
public abstract class ImmutableType<T> implements UserType, BasicType {

private final Configuration configuration;

Expand Down Expand Up @@ -338,4 +339,11 @@ public Object fromXMLNode(Node xml, Mapping factory) throws HibernateException {
Method valueOfMethod = ReflectionUtils.getMethodOrNull(clazz, "valueOf", String.class);
return valueOfMethod != null ? ReflectionUtils.invokeStaticMethod(valueOfMethod, xml.getText()) : null;
}

@Override
public String[] getRegistrationKeys() {
return new String[]{
getName()
};
}
}
Expand Up @@ -12,6 +12,7 @@
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.BasicType;
import org.hibernate.usertype.CompositeUserType;
import org.hibernate.usertype.UserType;

/**
Expand Down Expand Up @@ -77,17 +78,17 @@ public void contribute(TypeContributions typeContributions, ServiceRegistry serv
.contributeType(typeContributions, JsonType.INSTANCE);
}

private HibernateTypesContributor contributeType(TypeContributions typeContributions, BasicType type) {
typeContributions.contributeType(type);
return this;
}

private HibernateTypesContributor contributeType(TypeContributions typeContributions, UserType type) {
if(type instanceof ImmutableType) {
ImmutableType immutableType = (ImmutableType) type;
typeContributions.contributeType(immutableType, immutableType.getName());
private HibernateTypesContributor contributeType(TypeContributions typeContributions, Object type) {
if (type instanceof BasicType) {
typeContributions.contributeType((BasicType) type);
} else if (type instanceof UserType) {
typeContributions.contributeType((UserType) type, type.getClass().getSimpleName());
} else if (type instanceof CompositeUserType) {
typeContributions.contributeType((CompositeUserType) type, type.getClass().getSimpleName());
} else {
typeContributions.contributeType(type, type.getClass().getSimpleName());
throw new UnsupportedOperationException(
String.format("The [%s] is not supported!", type.getClass())
);
}
return this;
}
Expand Down
Expand Up @@ -8,6 +8,7 @@
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.type.BasicType;
import org.hibernate.type.ForeignKeyDirection;
import org.hibernate.type.Type;
import org.hibernate.type.descriptor.java.IncomparableComparator;
Expand All @@ -28,7 +29,7 @@
*
* @author Vlad Mihalcea
*/
public abstract class ImmutableType<T> implements UserType, Type {
public abstract class ImmutableType<T> implements UserType, BasicType {

private final Configuration configuration;

Expand Down Expand Up @@ -319,4 +320,11 @@ public Object replace(Object original, Object target, SessionImplementor session
public boolean[] toColumnNullness(Object value, Mapping mapping) {
return value == null ? ArrayHelper.FALSE : ArrayHelper.TRUE;
}

@Override
public String[] getRegistrationKeys() {
return new String[]{
getName()
};
}
}
9 changes: 9 additions & 0 deletions hibernate-types-52/pom.xml
Expand Up @@ -142,6 +142,15 @@
<optional>true</optional>
</dependency>

<!-- Testing -->

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>${hibernate.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
Expand Down
Expand Up @@ -6,6 +6,7 @@
import com.vladmihalcea.hibernate.type.interval.PostgreSQLIntervalType;
import com.vladmihalcea.hibernate.type.interval.PostgreSQLPeriodType;
import com.vladmihalcea.hibernate.type.json.*;
import com.vladmihalcea.hibernate.type.money.MonetaryAmountType;
import com.vladmihalcea.hibernate.type.range.PostgreSQLRangeType;
import com.vladmihalcea.hibernate.type.range.guava.PostgreSQLGuavaRangeType;
import com.vladmihalcea.hibernate.type.search.PostgreSQLTSVectorType;
Expand All @@ -16,6 +17,7 @@
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.BasicType;
import org.hibernate.usertype.CompositeUserType;
import org.hibernate.usertype.UserType;

/**
Expand Down Expand Up @@ -92,21 +94,22 @@ public void contribute(TypeContributions typeContributions, ServiceRegistry serv
.contributeType(typeContributions, YearMonthEpochType.INSTANCE)
.contributeType(typeContributions, YearMonthIntegerType.INSTANCE)
.contributeType(typeContributions, YearMonthTimestampType.INSTANCE)
.contributeType(typeContributions, MonetaryAmountType.INSTANCE)
/* JSON */
.contributeType(typeContributions, JsonType.INSTANCE);
}

private HibernateTypesContributor contributeType(TypeContributions typeContributions, BasicType type) {
typeContributions.contributeType(type);
return this;
}

private HibernateTypesContributor contributeType(TypeContributions typeContributions, UserType type) {
if(type instanceof ImmutableType) {
ImmutableType immutableType = (ImmutableType) type;
typeContributions.contributeType(immutableType, immutableType.getName());
private HibernateTypesContributor contributeType(TypeContributions typeContributions, Object type) {
if (type instanceof BasicType) {
typeContributions.contributeType((BasicType) type);
} else if (type instanceof UserType) {
typeContributions.contributeType((UserType) type, type.getClass().getSimpleName());
} else if (type instanceof CompositeUserType) {
typeContributions.contributeType((CompositeUserType) type, type.getClass().getSimpleName());
} else {
typeContributions.contributeType(type, type.getClass().getSimpleName());
throw new UnsupportedOperationException(
String.format("The [%s] is not supported!", type.getClass())
);
}
return this;
}
Expand Down
Expand Up @@ -8,6 +8,7 @@
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.type.BasicType;
import org.hibernate.type.ForeignKeyDirection;
import org.hibernate.type.Type;
import org.hibernate.type.descriptor.java.IncomparableComparator;
Expand All @@ -32,7 +33,7 @@
*
* @author Vlad Mihalcea
*/
public abstract class ImmutableCompositeType<T> implements CompositeUserType, Type {
public abstract class ImmutableCompositeType<T> implements CompositeUserType, BasicType {

private final Configuration configuration;

Expand Down Expand Up @@ -325,4 +326,11 @@ public Object replace(Object original, Object target, SharedSessionContractImple
public boolean[] toColumnNullness(Object value, Mapping mapping) {
return value == null ? ArrayHelper.FALSE : ArrayHelper.TRUE;
}

@Override
public String[] getRegistrationKeys() {
return new String[]{
getName()
};
}
}
Expand Up @@ -8,6 +8,7 @@
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.type.BasicType;
import org.hibernate.type.ForeignKeyDirection;
import org.hibernate.type.Type;
import org.hibernate.type.descriptor.java.IncomparableComparator;
Expand All @@ -28,7 +29,7 @@
*
* @author Vlad Mihalcea
*/
public abstract class ImmutableType<T> implements UserType, Type {
public abstract class ImmutableType<T> implements UserType, BasicType {

private final Configuration configuration;

Expand Down Expand Up @@ -319,4 +320,11 @@ public Object replace(Object original, Object target, SharedSessionContractImple
public boolean[] toColumnNullness(Object value, Mapping mapping) {
return value == null ? ArrayHelper.FALSE : ArrayHelper.TRUE;
}

@Override
public String[] getRegistrationKeys() {
return new String[]{
getName()
};
}
}
Expand Up @@ -30,6 +30,8 @@
*/
public class MonetaryAmountType extends ImmutableCompositeType<MonetaryAmount> {

public static final MonetaryAmountType INSTANCE = new MonetaryAmountType();

public MonetaryAmountType() {
super(MonetaryAmount.class);
}
Expand Down
Expand Up @@ -5,6 +5,7 @@
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.envers.Audited;
import org.junit.Test;

import javax.persistence.*;
Expand Down Expand Up @@ -67,6 +68,7 @@ public void test() {
});
}

@Audited
@Entity(name = "Book")
@Table(name = "book")
@TypeDef(name = "hstore", typeClass = PostgreSQLHStoreType.class)
Expand Down
11 changes: 10 additions & 1 deletion hibernate-types-55/pom.xml
Expand Up @@ -142,6 +142,15 @@
<optional>true</optional>
</dependency>

<!-- Testing -->

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>${hibernate.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
Expand All @@ -155,7 +164,7 @@
<properties>
<jdk.version>8</jdk.version>

<hibernate.version>5.6.9.Final</hibernate.version>
<hibernate.version>5.6.10.Final</hibernate.version>
<postgresql.version>42.3.3</postgresql.version>

<mysql.version>8.0.28</mysql.version>
Expand Down
Expand Up @@ -6,6 +6,7 @@
import com.vladmihalcea.hibernate.type.interval.PostgreSQLIntervalType;
import com.vladmihalcea.hibernate.type.interval.PostgreSQLPeriodType;
import com.vladmihalcea.hibernate.type.json.*;
import com.vladmihalcea.hibernate.type.money.MonetaryAmountType;
import com.vladmihalcea.hibernate.type.range.PostgreSQLRangeType;
import com.vladmihalcea.hibernate.type.range.guava.PostgreSQLGuavaRangeType;
import com.vladmihalcea.hibernate.type.search.PostgreSQLTSVectorType;
Expand All @@ -16,6 +17,7 @@
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.BasicType;
import org.hibernate.usertype.CompositeUserType;
import org.hibernate.usertype.UserType;

/**
Expand Down Expand Up @@ -92,21 +94,22 @@ public void contribute(TypeContributions typeContributions, ServiceRegistry serv
.contributeType(typeContributions, YearMonthEpochType.INSTANCE)
.contributeType(typeContributions, YearMonthIntegerType.INSTANCE)
.contributeType(typeContributions, YearMonthTimestampType.INSTANCE)
.contributeType(typeContributions, MonetaryAmountType.INSTANCE)
/* JSON */
.contributeType(typeContributions, JsonType.INSTANCE);
}

private HibernateTypesContributor contributeType(TypeContributions typeContributions, BasicType type) {
typeContributions.contributeType(type);
return this;
}

private HibernateTypesContributor contributeType(TypeContributions typeContributions, UserType type) {
if(type instanceof ImmutableType) {
ImmutableType immutableType = (ImmutableType) type;
typeContributions.contributeType(immutableType, immutableType.getName());
private HibernateTypesContributor contributeType(TypeContributions typeContributions, Object type) {
if (type instanceof BasicType) {
typeContributions.contributeType((BasicType) type);
} else if (type instanceof UserType) {
typeContributions.contributeType((UserType) type, type.getClass().getSimpleName());
} else if (type instanceof CompositeUserType) {
typeContributions.contributeType((CompositeUserType) type, type.getClass().getSimpleName());
} else {
typeContributions.contributeType(type, type.getClass().getSimpleName());
throw new UnsupportedOperationException(
String.format("The [%s] is not supported!", type.getClass())
);
}
return this;
}
Expand Down
Expand Up @@ -8,11 +8,11 @@
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.type.BasicType;
import org.hibernate.type.ForeignKeyDirection;
import org.hibernate.type.Type;
import org.hibernate.type.descriptor.java.IncomparableComparator;
import org.hibernate.usertype.CompositeUserType;
import org.hibernate.usertype.UserType;

import java.io.Serializable;
import java.sql.PreparedStatement;
Expand All @@ -33,7 +33,7 @@
*
* @author Vlad Mihalcea
*/
public abstract class ImmutableCompositeType<T> implements CompositeUserType, Type {
public abstract class ImmutableCompositeType<T> implements CompositeUserType, BasicType {

private final Configuration configuration;

Expand Down Expand Up @@ -326,4 +326,11 @@ public Object replace(Object original, Object target, SharedSessionContractImple
public boolean[] toColumnNullness(Object value, Mapping mapping) {
return value == null ? ArrayHelper.FALSE : ArrayHelper.TRUE;
}

@Override
public String[] getRegistrationKeys() {
return new String[]{
getName()
};
}
}

0 comments on commit e49879a

Please sign in to comment.