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 7299e4b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 14 deletions.
7 changes: 7 additions & 0 deletions hibernate-types-55/pom.xml
Expand Up @@ -24,6 +24,13 @@
<scope>provided</scope>
</dependency>

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

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</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,8 @@
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.BasicType;
import org.hibernate.type.Type;
import org.hibernate.usertype.CompositeUserType;
import org.hibernate.usertype.UserType;

/**
Expand Down Expand Up @@ -92,21 +95,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 <T extends Type> 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,9 @@ 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,10 +8,10 @@
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.type.spi.TypeBootstrapContext;
import org.hibernate.usertype.UserType;

import java.io.Serializable;
Expand All @@ -29,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 @@ -320,4 +320,9 @@ 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
Expand Up @@ -4,6 +4,7 @@
import org.hibernate.annotations.Columns;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.envers.Audited;
import org.javamoney.moneta.Money;
import org.junit.Test;

Expand Down Expand Up @@ -90,6 +91,7 @@ public void testReturnNullMoney() {
});
}

@Audited
@Entity(name = "Salary")
@Table(name = "salary")
@TypeDef(name = "monetary-amount-currency", typeClass = MonetaryAmountType.class, defaultForType = MonetaryAmount.class)
Expand Down

0 comments on commit 7299e4b

Please sign in to comment.