From c8876e884c3d8ea7820a318f099aa6b28757a0bc Mon Sep 17 00:00:00 2001
From: Sadeq <3616518+msdousti@users.noreply.github.com>
Date: Mon, 1 Aug 2022 20:12:10 +0200
Subject: [PATCH] [wip] Fix Envers compatibility with ImmutableType
Envers expects (simple) audited types to implement the BasicType
interface. This commit changes The ImmutableType to implement BasicType
rather than Type.
The commit is work in progress: It only applies the change to
hibernate-types-55 as a template. If the change is OK, it will be
applied to other hibernate-types-* as well.
Resolves: https://github.com/vladmihalcea/hibernate-types/issues/463
---
hibernate-types-55/pom.xml | 7 +++++++
.../hibernate/type/HibernateTypesContributor.java | 12 ++++++------
.../vladmihalcea/hibernate/type/ImmutableType.java | 9 +++++++--
.../type/basic/PostgreSQLHStoreTypeTest.java | 2 ++
4 files changed, 22 insertions(+), 8 deletions(-)
diff --git a/hibernate-types-55/pom.xml b/hibernate-types-55/pom.xml
index a88ce0b3e..9f5f47fff 100644
--- a/hibernate-types-55/pom.xml
+++ b/hibernate-types-55/pom.xml
@@ -24,6 +24,13 @@
provided
+
+ org.hibernate
+ hibernate-envers
+ ${hibernate.version}
+ test
+
+
com.fasterxml.jackson.core
jackson-databind
diff --git a/hibernate-types-55/src/main/java/com/vladmihalcea/hibernate/type/HibernateTypesContributor.java b/hibernate-types-55/src/main/java/com/vladmihalcea/hibernate/type/HibernateTypesContributor.java
index 6d99cf7ab..6dbcb966b 100644
--- a/hibernate-types-55/src/main/java/com/vladmihalcea/hibernate/type/HibernateTypesContributor.java
+++ b/hibernate-types-55/src/main/java/com/vladmihalcea/hibernate/type/HibernateTypesContributor.java
@@ -101,13 +101,13 @@ private HibernateTypesContributor contributeType(TypeContributions typeContribut
return this;
}
+ private HibernateTypesContributor contributeType(TypeContributions typeContributions, ImmutableType> 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());
- } else {
- typeContributions.contributeType(type, type.getClass().getSimpleName());
- }
+ typeContributions.contributeType(type, type.getClass().getSimpleName());
return this;
}
}
diff --git a/hibernate-types-55/src/main/java/com/vladmihalcea/hibernate/type/ImmutableType.java b/hibernate-types-55/src/main/java/com/vladmihalcea/hibernate/type/ImmutableType.java
index 50350ee4c..24cf79dd0 100644
--- a/hibernate-types-55/src/main/java/com/vladmihalcea/hibernate/type/ImmutableType.java
+++ b/hibernate-types-55/src/main/java/com/vladmihalcea/hibernate/type/ImmutableType.java
@@ -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;
@@ -29,7 +29,7 @@
*
* @author Vlad Mihalcea
*/
-public abstract class ImmutableType implements UserType, Type {
+public abstract class ImmutableType implements UserType, BasicType {
private final Configuration configuration;
@@ -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()};
+ }
}
\ No newline at end of file
diff --git a/hibernate-types-55/src/test/java/com/vladmihalcea/hibernate/type/basic/PostgreSQLHStoreTypeTest.java b/hibernate-types-55/src/test/java/com/vladmihalcea/hibernate/type/basic/PostgreSQLHStoreTypeTest.java
index 9d5a83f15..b4d5fe9b3 100644
--- a/hibernate-types-55/src/test/java/com/vladmihalcea/hibernate/type/basic/PostgreSQLHStoreTypeTest.java
+++ b/hibernate-types-55/src/test/java/com/vladmihalcea/hibernate/type/basic/PostgreSQLHStoreTypeTest.java
@@ -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.*;
@@ -67,6 +68,7 @@ public void test() {
});
}
+ @Audited
@Entity(name = "Book")
@Table(name = "book")
@TypeDef(name = "hstore", typeClass = PostgreSQLHStoreType.class)