From e06da88b4226cee14f7eb17e2217bafcc39a04f0 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Mon, 30 Sep 2024 11:37:18 +0200 Subject: [PATCH] HHH-17612 HHH-18762 Separate envers default revision entity types from mapped-superclasses Also, ensure they're query-able through Criteria and HQL queries --- .../source/internal/hbm/ModelBinder.java | 4 + .../envers/DefaultRevisionEntity.java | 70 +------------- ...rackingModifiedEntitiesRevisionEntity.java | 64 +------------ .../org/hibernate/envers/RevisionMapping.java | 79 +++++++++++++++ ...ackingModifiedEntitiesRevisionMapping.java | 76 +++++++++++++++ .../internal/RevisionInfoConfiguration.java | 18 +--- .../metadata/AbstractMetadataGenerator.java | 4 +- .../enhanced/SequenceIdRevisionEntity.java | 85 +---------------- .../enhanced/SequenceIdRevisionMapping.java | 95 +++++++++++++++++++ ...rackingModifiedEntitiesRevisionEntity.java | 68 +------------ ...ackingModifiedEntitiesRevisionMapping.java | 80 ++++++++++++++++ .../internal/ValidityAuditStrategy.java | 2 +- .../test/envers/CustomRevisionEntityTest.java | 4 +- ...ityTypeChangeAuditDefaultTrackingTest.java | 4 +- .../envers/EntityTypeChangeAuditTest.java | 4 +- .../ExtendedRevisionEntity.java | 4 +- .../reventity/InheritedRevEntity.java | 4 +- .../AbstractRevisionEntityRemovalTest.java | 3 +- 18 files changed, 365 insertions(+), 303 deletions(-) create mode 100644 hibernate-envers/src/main/java/org/hibernate/envers/RevisionMapping.java create mode 100644 hibernate-envers/src/main/java/org/hibernate/envers/TrackingModifiedEntitiesRevisionMapping.java create mode 100644 hibernate-envers/src/main/java/org/hibernate/envers/enhanced/SequenceIdRevisionMapping.java create mode 100644 hibernate-envers/src/main/java/org/hibernate/envers/enhanced/SequenceIdTrackingModifiedEntitiesRevisionMapping.java diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java index bf4dd8567e..6a90fe7293 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java @@ -364,6 +364,10 @@ public class ModelBinder { entityDescriptor.setEntityName( entitySource.getEntityNamingSource().getEntityName() ); entityDescriptor.setJpaEntityName( entitySource.getEntityNamingSource().getJpaEntityName() ); entityDescriptor.setClassName( entitySource.getEntityNamingSource().getClassName() ); + if ( entityDescriptor.getJpaEntityName() != null && entityDescriptor.getClassName() != null ) { + metadataBuildingContext.getMetadataCollector() + .addImport( entityDescriptor.getJpaEntityName(), entityDescriptor.getClassName() ); + } entityDescriptor.setDiscriminatorValue( entitySource.getDiscriminatorMatchValue() != null diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/DefaultRevisionEntity.java b/hibernate-envers/src/main/java/org/hibernate/envers/DefaultRevisionEntity.java index 8d5991e324..2aefe1337c 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/DefaultRevisionEntity.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/DefaultRevisionEntity.java @@ -4,76 +4,12 @@ */ package org.hibernate.envers; -import java.io.Serializable; -import java.util.Date; - -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.Id; -import jakarta.persistence.MappedSuperclass; -import jakarta.persistence.Transient; +import jakarta.persistence.Entity; /** * @author Adam Warski (adam at warski dot org) * @author Chris Cranford */ -@MappedSuperclass -public class DefaultRevisionEntity implements Serializable { - private static final long serialVersionUID = 8530213963961662300L; - - @Id - @GeneratedValue - @RevisionNumber - private int id; - - @RevisionTimestamp - private long timestamp; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - @Transient - public Date getRevisionDate() { - return new Date( timestamp ); - } - - public long getTimestamp() { - return timestamp; - } - - public void setTimestamp(long timestamp) { - this.timestamp = timestamp; - } - - @Override - public boolean equals(Object o) { - if ( this == o ) { - return true; - } - if ( !(o instanceof DefaultRevisionEntity) ) { - return false; - } - - final DefaultRevisionEntity that = (DefaultRevisionEntity) o; - return id == that.id - && timestamp == that.timestamp; - } - - @Override - public int hashCode() { - int result; - result = id; - result = 31 * result + (int) (timestamp ^ (timestamp >>> 32)); - return result; - } - - @Override - public String toString() { - return "DefaultRevisionEntity(id = " + id - + ", revisionDate = " + DateTimeFormatter.INSTANCE.format(getRevisionDate() ) + ")"; - } +@Entity +public final class DefaultRevisionEntity extends RevisionMapping { } diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/DefaultTrackingModifiedEntitiesRevisionEntity.java b/hibernate-envers/src/main/java/org/hibernate/envers/DefaultTrackingModifiedEntitiesRevisionEntity.java index e5cd2a80d2..0c4e6cd1ae 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/DefaultTrackingModifiedEntitiesRevisionEntity.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/DefaultTrackingModifiedEntitiesRevisionEntity.java @@ -4,17 +4,7 @@ */ package org.hibernate.envers; -import java.util.HashSet; -import java.util.Set; -import jakarta.persistence.Column; -import jakarta.persistence.ElementCollection; -import jakarta.persistence.FetchType; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.JoinTable; -import jakarta.persistence.MappedSuperclass; - -import org.hibernate.annotations.Fetch; -import org.hibernate.annotations.FetchMode; +import jakarta.persistence.Entity; /** * Extension of standard {@link DefaultRevisionEntity} that allows tracking entity names changed in each revision. @@ -23,54 +13,6 @@ import org.hibernate.annotations.FetchMode; * * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) */ -@MappedSuperclass -public class DefaultTrackingModifiedEntitiesRevisionEntity extends DefaultRevisionEntity { - @ElementCollection(fetch = FetchType.EAGER) - @JoinTable(name = "REVCHANGES", joinColumns = @JoinColumn(name = "REV")) - @Column(name = "ENTITYNAME") - @Fetch(FetchMode.JOIN) - @ModifiedEntityNames - private Set modifiedEntityNames = new HashSet<>(); - - public Set getModifiedEntityNames() { - return modifiedEntityNames; - } - - public void setModifiedEntityNames(Set modifiedEntityNames) { - this.modifiedEntityNames = modifiedEntityNames; - } - - @Override - public boolean equals(Object o) { - if ( this == o ) { - return true; - } - if ( !(o instanceof DefaultTrackingModifiedEntitiesRevisionEntity) ) { - return false; - } - if ( !super.equals( o ) ) { - return false; - } - - final DefaultTrackingModifiedEntitiesRevisionEntity that = (DefaultTrackingModifiedEntitiesRevisionEntity) o; - - if ( modifiedEntityNames != null ? !modifiedEntityNames.equals( that.modifiedEntityNames ) - : that.modifiedEntityNames != null ) { - return false; - } - - return true; - } - - @Override - public int hashCode() { - int result = super.hashCode(); - result = 31 * result + (modifiedEntityNames != null ? modifiedEntityNames.hashCode() : 0); - return result; - } - - @Override - public String toString() { - return "DefaultTrackingModifiedEntitiesRevisionEntity(" + super.toString() + ", modifiedEntityNames = " + modifiedEntityNames + ")"; - } +@Entity +public final class DefaultTrackingModifiedEntitiesRevisionEntity extends TrackingModifiedEntitiesRevisionMapping { } diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/RevisionMapping.java b/hibernate-envers/src/main/java/org/hibernate/envers/RevisionMapping.java new file mode 100644 index 0000000000..da1351d026 --- /dev/null +++ b/hibernate-envers/src/main/java/org/hibernate/envers/RevisionMapping.java @@ -0,0 +1,79 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.envers; + +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.MappedSuperclass; +import jakarta.persistence.Transient; + +import java.io.Serializable; +import java.util.Date; + +/** + * @author Adam Warski (adam at warski dot org) + * @author Chris Cranford + */ +@MappedSuperclass +public class RevisionMapping implements Serializable { + private static final long serialVersionUID = 8530213963961662300L; + + @Id + @GeneratedValue + @RevisionNumber + private int id; + + @RevisionTimestamp + private long timestamp; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + @Transient + public Date getRevisionDate() { + return new Date( timestamp ); + } + + public long getTimestamp() { + return timestamp; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( !(o instanceof RevisionMapping) ) { + return false; + } + + final RevisionMapping that = (RevisionMapping) o; + return id == that.id + && timestamp == that.timestamp; + } + + @Override + public int hashCode() { + int result; + result = id; + result = 31 * result + (int) (timestamp ^ (timestamp >>> 32)); + return result; + } + + @Override + public String toString() { + return "DefaultRevisionEntity(id = " + id + + ", revisionDate = " + DateTimeFormatter.INSTANCE.format(getRevisionDate() ) + ")"; + } +} diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/TrackingModifiedEntitiesRevisionMapping.java b/hibernate-envers/src/main/java/org/hibernate/envers/TrackingModifiedEntitiesRevisionMapping.java new file mode 100644 index 0000000000..37c8159742 --- /dev/null +++ b/hibernate-envers/src/main/java/org/hibernate/envers/TrackingModifiedEntitiesRevisionMapping.java @@ -0,0 +1,76 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.envers; + +import jakarta.persistence.Column; +import jakarta.persistence.ElementCollection; +import jakarta.persistence.FetchType; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.JoinTable; +import jakarta.persistence.MappedSuperclass; +import org.hibernate.annotations.Fetch; +import org.hibernate.annotations.FetchMode; + +import java.util.HashSet; +import java.util.Set; + +/** + * Extension of standard {@link RevisionMapping} that allows tracking entity names changed in each revision. + * This revision entity is implicitly used when {@code org.hibernate.envers.track_entities_changed_in_revision} + * parameter is set to {@code true}. + * + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +@MappedSuperclass +public class TrackingModifiedEntitiesRevisionMapping extends RevisionMapping { + @ElementCollection(fetch = FetchType.EAGER) + @JoinTable(name = "REVCHANGES", joinColumns = @JoinColumn(name = "REV")) + @Column(name = "ENTITYNAME") + @Fetch(FetchMode.JOIN) + @ModifiedEntityNames + private Set modifiedEntityNames = new HashSet<>(); + + public Set getModifiedEntityNames() { + return modifiedEntityNames; + } + + public void setModifiedEntityNames(Set modifiedEntityNames) { + this.modifiedEntityNames = modifiedEntityNames; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( !(o instanceof TrackingModifiedEntitiesRevisionMapping) ) { + return false; + } + if ( !super.equals( o ) ) { + return false; + } + + final TrackingModifiedEntitiesRevisionMapping that = (TrackingModifiedEntitiesRevisionMapping) o; + + if ( modifiedEntityNames != null ? !modifiedEntityNames.equals( that.modifiedEntityNames ) + : that.modifiedEntityNames != null ) { + return false; + } + + return true; + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + (modifiedEntityNames != null ? modifiedEntityNames.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "DefaultTrackingModifiedEntitiesRevisionEntity(" + super.toString() + ", modifiedEntityNames = " + modifiedEntityNames + ")"; + } +} diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/RevisionInfoConfiguration.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/RevisionInfoConfiguration.java index 5ab6c98102..7dfedb2250 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/RevisionInfoConfiguration.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/RevisionInfoConfiguration.java @@ -166,7 +166,7 @@ public class RevisionInfoConfiguration { true, false, true, - revisionInfoEntityName + revisionInfoClass.getName() ); attribute.setOnDelete( configuration.isCascadeDeleteRevision() ? "cascade" : null ); @@ -190,7 +190,7 @@ public class RevisionInfoConfiguration { RootPersistentEntity mapping = new RootPersistentEntity( new AuditTableData( null, null, configuration.getDefaultSchemaName(), configuration.getDefaultCatalogName() ), revisionInfoClass, - revisionInfoEntityName, + useDefaultRevisionInfoMapping ? null : revisionInfoEntityName, DEFAULT_REVISION_ENTITY_TABLE_NAME ); @@ -283,7 +283,6 @@ public class RevisionInfoConfiguration { public RevisionEntityResolver(InFlightMetadataCollector metadata) { this.metadata = metadata; - this.revisionInfoEntityName = getDefaultEntityName(); this.revisionInfoIdData = createPropertyData( "id", "field" ); this.revisionInfoTimestampData = createPropertyData( "timestamp", "field" ); this.modifiedEntityNamesData = createPropertyData( "modifiedEntityNames", "field" ); @@ -294,15 +293,6 @@ public class RevisionInfoConfiguration { locateRevisionEntityMapping(); } - private String getDefaultEntityName() { - if ( configuration.isNativeIdEnabled() ) { - return DefaultRevisionEntity.class.getName(); - } - else { - return SequenceIdRevisionEntity.class.getName(); - } - } - private void locateRevisionEntityMapping() { for ( PersistentClass persistentClass : metadata.getEntityBindings() ) { // Only process POJO models, not dynamic models @@ -390,7 +380,6 @@ public class RevisionInfoConfiguration { revisionInfoClass = configuration.isNativeIdEnabled() ? DefaultTrackingModifiedEntitiesRevisionEntity.class : SequenceIdTrackingModifiedEntitiesRevisionEntity.class; - revisionInfoEntityName = revisionInfoClass.getName(); } else { revisionInfoClass = configuration.isNativeIdEnabled() @@ -398,6 +387,9 @@ public class RevisionInfoConfiguration { : SequenceIdRevisionEntity.class; } + // Use the simple name of default revision entities as entity name + revisionInfoEntityName = revisionInfoClass.getSimpleName(); + timestampValueResolver = createRevisionTimestampResolver( revisionInfoClass, revisionInfoTimestampData, diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/AbstractMetadataGenerator.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/AbstractMetadataGenerator.java index 2409a256b4..3131dd2afb 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/AbstractMetadataGenerator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/AbstractMetadataGenerator.java @@ -109,7 +109,7 @@ public abstract class AbstractMetadataGenerator { entity, metadataBuildingContext.getConfiguration(), metadataBuildingContext.getConfiguration().getRevisionTypePropertyType(), - metadataBuildingContext.getConfiguration().getRevisionInfo().getRevisionInfoEntityName(), + metadataBuildingContext.getConfiguration().getRevisionInfo().getRevisionInfoClass().getName(), false ) ); @@ -123,7 +123,7 @@ public abstract class AbstractMetadataGenerator { entity, metadataBuildingContext.getConfiguration(), metadataBuildingContext.getConfiguration().getRevisionTypePropertyType(), - metadataBuildingContext.getConfiguration().getRevisionInfo().getRevisionInfoEntityName(), + metadataBuildingContext.getConfiguration().getRevisionInfo().getRevisionInfoClass().getName(), true ) ); diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/enhanced/SequenceIdRevisionEntity.java b/hibernate-envers/src/main/java/org/hibernate/envers/enhanced/SequenceIdRevisionEntity.java index a31dfdf76d..f281a16dcf 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/enhanced/SequenceIdRevisionEntity.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/enhanced/SequenceIdRevisionEntity.java @@ -4,92 +4,13 @@ */ package org.hibernate.envers.enhanced; -import java.io.Serializable; -import java.util.Date; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.Id; -import jakarta.persistence.MappedSuperclass; -import jakarta.persistence.Transient; - -import org.hibernate.annotations.GenericGenerator; -import org.hibernate.annotations.Parameter; -import org.hibernate.envers.DateTimeFormatter; -import org.hibernate.envers.RevisionNumber; -import org.hibernate.envers.RevisionTimestamp; +import jakarta.persistence.Entity; /** * @author Adam Warski (adam at warski dot org) * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) * @author Chris Cranford */ -@MappedSuperclass -public class SequenceIdRevisionEntity implements Serializable { - private static final long serialVersionUID = 4159156677698841902L; - - @Id - @GeneratedValue(generator = "RevisionNumberSequenceGenerator") - @GenericGenerator( - name = "RevisionNumberSequenceGenerator", - strategy = "org.hibernate.envers.enhanced.OrderedSequenceGenerator", - parameters = { - @Parameter(name = "table_name", value = "REVISION_GENERATOR"), - @Parameter(name = "sequence_name", value = "REVISION_GENERATOR"), - @Parameter(name = "initial_value", value = "1"), - @Parameter(name = "increment_size", value = "1") - } - ) - @RevisionNumber - private int id; - - @RevisionTimestamp - private long timestamp; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - @Transient - public Date getRevisionDate() { - return new Date( timestamp ); - } - - public long getTimestamp() { - return timestamp; - } - - @SuppressWarnings("unused") - public void setTimestamp(long timestamp) { - this.timestamp = timestamp; - } - - @Override - public boolean equals(Object o) { - if ( this == o ) { - return true; - } - if ( !(o instanceof SequenceIdRevisionEntity) ) { - return false; - } - - final SequenceIdRevisionEntity that = (SequenceIdRevisionEntity) o; - return id == that.id && timestamp == that.timestamp; - } - - @Override - public int hashCode() { - int result = id; - result = 31 * result + (int) (timestamp ^ (timestamp >>> 32)); - return result; - } - - @Override - public String toString() { - return "SequenceIdRevisionEntity(id = " + id - + ", revisionDate = " + DateTimeFormatter.INSTANCE.format(getRevisionDate() ) - + ")"; - } +@Entity +public final class SequenceIdRevisionEntity extends SequenceIdRevisionMapping { } diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/enhanced/SequenceIdRevisionMapping.java b/hibernate-envers/src/main/java/org/hibernate/envers/enhanced/SequenceIdRevisionMapping.java new file mode 100644 index 0000000000..ddbae7f57b --- /dev/null +++ b/hibernate-envers/src/main/java/org/hibernate/envers/enhanced/SequenceIdRevisionMapping.java @@ -0,0 +1,95 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.envers.enhanced; + +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.MappedSuperclass; +import jakarta.persistence.Transient; +import org.hibernate.annotations.GenericGenerator; +import org.hibernate.annotations.Parameter; +import org.hibernate.envers.DateTimeFormatter; +import org.hibernate.envers.RevisionNumber; +import org.hibernate.envers.RevisionTimestamp; + +import java.io.Serializable; +import java.util.Date; + +/** + * @author Adam Warski (adam at warski dot org) + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + * @author Chris Cranford + */ +@MappedSuperclass +public class SequenceIdRevisionMapping implements Serializable { + private static final long serialVersionUID = 4159156677698841902L; + + @Id + @GeneratedValue(generator = "RevisionNumberSequenceGenerator") + @GenericGenerator( + name = "RevisionNumberSequenceGenerator", + strategy = "org.hibernate.envers.enhanced.OrderedSequenceGenerator", + parameters = { + @Parameter(name = "table_name", value = "REVISION_GENERATOR"), + @Parameter(name = "sequence_name", value = "REVISION_GENERATOR"), + @Parameter(name = "initial_value", value = "1"), + @Parameter(name = "increment_size", value = "1") + } + ) + @RevisionNumber + private int id; + + @RevisionTimestamp + private long timestamp; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + @Transient + public Date getRevisionDate() { + return new Date( timestamp ); + } + + public long getTimestamp() { + return timestamp; + } + + @SuppressWarnings("unused") + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( !(o instanceof SequenceIdRevisionMapping) ) { + return false; + } + + final SequenceIdRevisionMapping that = (SequenceIdRevisionMapping) o; + return id == that.id && timestamp == that.timestamp; + } + + @Override + public int hashCode() { + int result = id; + result = 31 * result + (int) (timestamp ^ (timestamp >>> 32)); + return result; + } + + @Override + public String toString() { + return "SequenceIdRevisionEntity(id = " + id + + ", revisionDate = " + DateTimeFormatter.INSTANCE.format(getRevisionDate() ) + + ")"; + } +} diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/enhanced/SequenceIdTrackingModifiedEntitiesRevisionEntity.java b/hibernate-envers/src/main/java/org/hibernate/envers/enhanced/SequenceIdTrackingModifiedEntitiesRevisionEntity.java index 36df1e35ad..21e3d8bfb5 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/enhanced/SequenceIdTrackingModifiedEntitiesRevisionEntity.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/enhanced/SequenceIdTrackingModifiedEntitiesRevisionEntity.java @@ -4,18 +4,7 @@ */ package org.hibernate.envers.enhanced; -import java.util.HashSet; -import java.util.Set; -import jakarta.persistence.Column; -import jakarta.persistence.ElementCollection; -import jakarta.persistence.FetchType; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.JoinTable; -import jakarta.persistence.MappedSuperclass; - -import org.hibernate.annotations.Fetch; -import org.hibernate.annotations.FetchMode; -import org.hibernate.envers.ModifiedEntityNames; +import jakarta.persistence.Entity; /** * Extension of standard {@link SequenceIdRevisionEntity} that allows tracking entity names changed in each revision. @@ -24,57 +13,6 @@ import org.hibernate.envers.ModifiedEntityNames; * * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) */ -@MappedSuperclass -public class SequenceIdTrackingModifiedEntitiesRevisionEntity extends SequenceIdRevisionEntity { - @ElementCollection(fetch = FetchType.EAGER) - @JoinTable(name = "REVCHANGES", joinColumns = @JoinColumn(name = "REV")) - @Column(name = "ENTITYNAME") - @Fetch(FetchMode.JOIN) - @ModifiedEntityNames - private Set modifiedEntityNames = new HashSet<>(); - - @SuppressWarnings("unused") - public Set getModifiedEntityNames() { - return modifiedEntityNames; - } - - @SuppressWarnings("unused") - public void setModifiedEntityNames(Set modifiedEntityNames) { - this.modifiedEntityNames = modifiedEntityNames; - } - - @Override - public boolean equals(Object o) { - if ( this == o ) { - return true; - } - if ( !(o instanceof SequenceIdTrackingModifiedEntitiesRevisionEntity) ) { - return false; - } - if ( !super.equals( o ) ) { - return false; - } - - final SequenceIdTrackingModifiedEntitiesRevisionEntity that = (SequenceIdTrackingModifiedEntitiesRevisionEntity) o; - - if ( modifiedEntityNames == null ) { - return that.modifiedEntityNames == null; - } - else { - return modifiedEntityNames.equals( that.modifiedEntityNames ); - } - } - - @Override - public int hashCode() { - int result = super.hashCode(); - result = 31 * result + (modifiedEntityNames != null ? modifiedEntityNames.hashCode() : 0); - return result; - } - - @Override - public String toString() { - return "SequenceIdTrackingModifiedEntitiesRevisionEntity(" + super.toString() - + ", modifiedEntityNames = " + modifiedEntityNames + ")"; - } +@Entity +public final class SequenceIdTrackingModifiedEntitiesRevisionEntity extends SequenceIdTrackingModifiedEntitiesRevisionMapping { } diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/enhanced/SequenceIdTrackingModifiedEntitiesRevisionMapping.java b/hibernate-envers/src/main/java/org/hibernate/envers/enhanced/SequenceIdTrackingModifiedEntitiesRevisionMapping.java new file mode 100644 index 0000000000..dd4ff098b4 --- /dev/null +++ b/hibernate-envers/src/main/java/org/hibernate/envers/enhanced/SequenceIdTrackingModifiedEntitiesRevisionMapping.java @@ -0,0 +1,80 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.envers.enhanced; + +import jakarta.persistence.Column; +import jakarta.persistence.ElementCollection; +import jakarta.persistence.FetchType; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.JoinTable; +import jakarta.persistence.MappedSuperclass; +import org.hibernate.annotations.Fetch; +import org.hibernate.annotations.FetchMode; +import org.hibernate.envers.ModifiedEntityNames; + +import java.util.HashSet; +import java.util.Set; + +/** + * Extension of standard {@link SequenceIdRevisionMapping} that allows tracking entity names changed in each revision. + * This revision entity is implicitly used when {@code org.hibernate.envers.track_entities_changed_in_revision} + * parameter is set to {@code true}. + * + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +@MappedSuperclass +public class SequenceIdTrackingModifiedEntitiesRevisionMapping extends SequenceIdRevisionMapping { + @ElementCollection(fetch = FetchType.EAGER) + @JoinTable(name = "REVCHANGES", joinColumns = @JoinColumn(name = "REV")) + @Column(name = "ENTITYNAME") + @Fetch(FetchMode.JOIN) + @ModifiedEntityNames + private Set modifiedEntityNames = new HashSet<>(); + + @SuppressWarnings("unused") + public Set getModifiedEntityNames() { + return modifiedEntityNames; + } + + @SuppressWarnings("unused") + public void setModifiedEntityNames(Set modifiedEntityNames) { + this.modifiedEntityNames = modifiedEntityNames; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) { + return true; + } + if ( !(o instanceof SequenceIdTrackingModifiedEntitiesRevisionMapping) ) { + return false; + } + if ( !super.equals( o ) ) { + return false; + } + + final SequenceIdTrackingModifiedEntitiesRevisionMapping that = (SequenceIdTrackingModifiedEntitiesRevisionMapping) o; + + if ( modifiedEntityNames == null ) { + return that.modifiedEntityNames == null; + } + else { + return modifiedEntityNames.equals( that.modifiedEntityNames ); + } + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + (modifiedEntityNames != null ? modifiedEntityNames.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "SequenceIdTrackingModifiedEntitiesRevisionEntity(" + super.toString() + + ", modifiedEntityNames = " + modifiedEntityNames + ")"; + } +} diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/strategy/internal/ValidityAuditStrategy.java b/hibernate-envers/src/main/java/org/hibernate/envers/strategy/internal/ValidityAuditStrategy.java index d4cf254756..c938c1bafb 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/strategy/internal/ValidityAuditStrategy.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/strategy/internal/ValidityAuditStrategy.java @@ -547,7 +547,7 @@ public class ValidityAuditStrategy implements AuditStrategy { final EntityPersister rootEntity = getEntityPersister( entity.getRootEntityName(), session ); final EntityPersister auditEntity = getEntityPersister( auditEntityName, session ); final EntityPersister rootAuditEntity = getEntityPersister( auditEntity.getRootEntityName(), session ); - final EntityPersister revisionEntity = getEntityPersister( configuration.getRevisionInfo().getRevisionInfoEntityName(), session ); + final EntityPersister revisionEntity = getEntityPersister( configuration.getRevisionInfo().getRevisionInfoClass().getName(), session ); final Number revisionNumber = getRevisionNumber( configuration, revision ); diff --git a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/CustomRevisionEntityTest.java b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/CustomRevisionEntityTest.java index 03b0c1fb64..561a551a7c 100644 --- a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/CustomRevisionEntityTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/CustomRevisionEntityTest.java @@ -8,7 +8,7 @@ import java.util.Date; import org.hibernate.annotations.CreationTimestamp; import org.hibernate.envers.Audited; -import org.hibernate.envers.DefaultRevisionEntity; +import org.hibernate.envers.RevisionMapping; import org.hibernate.envers.RevisionEntity; import org.hibernate.envers.RevisionListener; @@ -124,7 +124,7 @@ public class CustomRevisionEntityTest { @Entity(name = "CustomRevisionEntity") @Table(name = "CUSTOM_REV_INFO") @RevisionEntity(CustomRevisionEntityListener.class) - public static class CustomRevisionEntity extends DefaultRevisionEntity { + public static class CustomRevisionEntity extends RevisionMapping { private String username; diff --git a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/EntityTypeChangeAuditDefaultTrackingTest.java b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/EntityTypeChangeAuditDefaultTrackingTest.java index f81a8d7d0b..1f856e7d1f 100644 --- a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/EntityTypeChangeAuditDefaultTrackingTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/EntityTypeChangeAuditDefaultTrackingTest.java @@ -19,7 +19,7 @@ import org.hibernate.annotations.CreationTimestamp; import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.envers.Audited; -import org.hibernate.envers.DefaultTrackingModifiedEntitiesRevisionEntity; +import org.hibernate.envers.TrackingModifiedEntitiesRevisionMapping; import org.hibernate.envers.RevisionEntity; import org.hibernate.jpa.boot.spi.Bootstrap; @@ -198,7 +198,7 @@ public class EntityTypeChangeAuditDefaultTrackingTest extends EntityManagerFacto @Table(name = "TRACKING_REV_INFO") @RevisionEntity public static class CustomTrackingRevisionEntity - extends DefaultTrackingModifiedEntitiesRevisionEntity { + extends TrackingModifiedEntitiesRevisionMapping { } //end::envers-tracking-modified-entities-revchanges-example[] diff --git a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/EntityTypeChangeAuditTest.java b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/EntityTypeChangeAuditTest.java index cd6a778f62..b43288e739 100644 --- a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/EntityTypeChangeAuditTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/EntityTypeChangeAuditTest.java @@ -25,7 +25,7 @@ import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.envers.AuditReaderFactory; import org.hibernate.envers.Audited; -import org.hibernate.envers.DefaultRevisionEntity; +import org.hibernate.envers.RevisionMapping; import org.hibernate.envers.ModifiedEntityNames; import org.hibernate.envers.RevisionEntity; import org.hibernate.jpa.boot.spi.Bootstrap; @@ -238,7 +238,7 @@ public class EntityTypeChangeAuditTest extends EntityManagerFactoryBasedFunction @Entity(name = "CustomTrackingRevisionEntity") @Table(name = "TRACKING_REV_INFO") @RevisionEntity - public static class CustomTrackingRevisionEntity extends DefaultRevisionEntity { + public static class CustomTrackingRevisionEntity extends RevisionMapping { @ElementCollection @JoinTable( diff --git a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/entities/reventity/trackmodifiedentities/ExtendedRevisionEntity.java b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/entities/reventity/trackmodifiedentities/ExtendedRevisionEntity.java index 582caaed9b..ffe4e352c7 100644 --- a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/entities/reventity/trackmodifiedentities/ExtendedRevisionEntity.java +++ b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/entities/reventity/trackmodifiedentities/ExtendedRevisionEntity.java @@ -8,14 +8,14 @@ import jakarta.persistence.Column; import jakarta.persistence.Entity; import org.hibernate.envers.RevisionEntity; -import org.hibernate.envers.enhanced.SequenceIdTrackingModifiedEntitiesRevisionEntity; +import org.hibernate.envers.enhanced.SequenceIdTrackingModifiedEntitiesRevisionMapping; /** * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) */ @Entity @RevisionEntity(ExtendedRevisionListener.class) -public class ExtendedRevisionEntity extends SequenceIdTrackingModifiedEntitiesRevisionEntity { +public class ExtendedRevisionEntity extends SequenceIdTrackingModifiedEntitiesRevisionMapping { @Column(name = "USER_COMMENT") private String comment; diff --git a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/reventity/InheritedRevEntity.java b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/reventity/InheritedRevEntity.java index 26f2319650..23c57d946f 100644 --- a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/reventity/InheritedRevEntity.java +++ b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/reventity/InheritedRevEntity.java @@ -7,12 +7,12 @@ package org.hibernate.orm.test.envers.integration.reventity; import jakarta.persistence.Entity; import org.hibernate.envers.RevisionEntity; -import org.hibernate.envers.enhanced.SequenceIdRevisionEntity; +import org.hibernate.envers.enhanced.SequenceIdRevisionMapping; /** * @author Adam Warski (adam at warski dot org) */ @Entity @RevisionEntity -public class InheritedRevEntity extends SequenceIdRevisionEntity { +public class InheritedRevEntity extends SequenceIdRevisionMapping { } diff --git a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/reventity/removal/AbstractRevisionEntityRemovalTest.java b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/reventity/removal/AbstractRevisionEntityRemovalTest.java index 07c4d1f89d..6aa24f1264 100644 --- a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/reventity/removal/AbstractRevisionEntityRemovalTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/reventity/removal/AbstractRevisionEntityRemovalTest.java @@ -34,8 +34,7 @@ public abstract class AbstractRevisionEntityRemovalTest extends BaseEnversJPAFun @Override protected Class[] getAnnotatedClasses() { return new Class[] { - StrTestEntity.class, ListOwnedEntity.class, ListOwningEntity.class, - getRevisionEntityClass() + StrTestEntity.class, ListOwnedEntity.class, ListOwningEntity.class }; }