HHH-15131 Fix JPA Compliance issue with Envers

This commit is contained in:
Christian Beikov 2022-03-17 17:13:10 +01:00
parent cb3690466f
commit 94f450aa7a
4 changed files with 80 additions and 5 deletions

View File

@ -31,6 +31,7 @@ public class RootPersistentEntity extends PersistentEntity implements JoinAwareP
private Identifier identifier; private Identifier identifier;
private String className; private String className;
private String entityName;
private String tableName; private String tableName;
private String whereClause; private String whereClause;
private DiscriminatorType discriminator; private DiscriminatorType discriminator;
@ -42,11 +43,12 @@ public class RootPersistentEntity extends PersistentEntity implements JoinAwareP
this.joins = new ArrayList<>(); this.joins = new ArrayList<>();
} }
public RootPersistentEntity(AuditTableData auditTableData, String className, String tableName) { public RootPersistentEntity(AuditTableData auditTableData, Class<?> clazz, String entityName, String tableName) {
super( auditTableData, null ); super( auditTableData, null );
this.attributes = new ArrayList<>(); this.attributes = new ArrayList<>();
this.joins = new ArrayList<>(); this.joins = new ArrayList<>();
this.className = className; this.className = clazz.getName();
this.entityName = entityName;
this.tableName = tableName; this.tableName = tableName;
} }
@ -116,6 +118,9 @@ public class RootPersistentEntity extends PersistentEntity implements JoinAwareP
} }
else if ( !StringTools.isEmpty( className ) ) { else if ( !StringTools.isEmpty( className ) ) {
entity.setName( className ); entity.setName( className );
if ( !StringTools.isEmpty( entityName ) ) {
entity.setEntityName( entityName );
}
} }
if ( !StringTools.isEmpty( getAuditTableData().getAuditTableName() ) ) { if ( !StringTools.isEmpty( getAuditTableData().getAuditTableName() ) ) {

View File

@ -165,7 +165,8 @@ public class RevisionInfoConfiguration {
true, true,
false, false,
true, true,
revisionInfoEntityName ); revisionInfoEntityName
);
attribute.setOnDelete( configuration.isCascadeDeleteRevision() ? "cascade" : null ); attribute.setOnDelete( configuration.isCascadeDeleteRevision() ? "cascade" : null );
@ -187,6 +188,7 @@ public class RevisionInfoConfiguration {
private RootPersistentEntity generateDefaultRevisionInfoMapping(String revisionInfoIdName) { private RootPersistentEntity generateDefaultRevisionInfoMapping(String revisionInfoIdName) {
RootPersistentEntity mapping = new RootPersistentEntity( RootPersistentEntity mapping = new RootPersistentEntity(
new AuditTableData( null, null, configuration.getDefaultSchemaName(), configuration.getDefaultCatalogName() ), new AuditTableData( null, null, configuration.getDefaultSchemaName(), configuration.getDefaultCatalogName() ),
revisionInfoClass,
revisionInfoEntityName, revisionInfoEntityName,
DEFAULT_REVISION_ENTITY_TABLE_NAME DEFAULT_REVISION_ENTITY_TABLE_NAME
); );

View File

@ -111,7 +111,7 @@ public abstract class AbstractMetadataGenerator {
entity, entity,
metadataBuildingContext.getConfiguration(), metadataBuildingContext.getConfiguration(),
metadataBuildingContext.getConfiguration().getRevisionTypePropertyType(), metadataBuildingContext.getConfiguration().getRevisionTypePropertyType(),
metadataBuildingContext.getConfiguration().getRevisionInfo().getRevisionInfoClass().getName(), metadataBuildingContext.getConfiguration().getRevisionInfo().getRevisionInfoEntityName(),
false false
) )
); );
@ -125,7 +125,7 @@ public abstract class AbstractMetadataGenerator {
entity, entity,
metadataBuildingContext.getConfiguration(), metadataBuildingContext.getConfiguration(),
metadataBuildingContext.getConfiguration().getRevisionTypePropertyType(), metadataBuildingContext.getConfiguration().getRevisionTypePropertyType(),
metadataBuildingContext.getConfiguration().getRevisionInfo().getRevisionInfoClass().getName(), metadataBuildingContext.getConfiguration().getRevisionInfo().getRevisionInfoEntityName(),
true true
) )
); );

View File

@ -0,0 +1,68 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.envers.test.integration.query;
import java.util.Map;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.envers.AuditReaderFactory;
import org.hibernate.envers.Audited;
import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase;
import org.hibernate.testing.transaction.TransactionUtil;
import org.junit.Test;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@SuppressWarnings("unchecked")
public class StrictJpaComplianceTest extends BaseEnversJPAFunctionalTestCase {
@Override
protected void addConfigOptions(Map options) {
super.addConfigOptions( options );
options.put( AvailableSettings.JPA_QUERY_COMPLIANCE, "true" );
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
Organization.class
};
}
@Test
public void testIt() {
TransactionUtil.doInJPA( this::entityManagerFactory, entityManager -> {
AuditReaderFactory.get( entityManager ).getRevisions( Organization.class, 1 );
} );
}
/**
* @author Madhumita Sadhukhan
*/
@Entity
@Table(name = "ORG")
public static class Organization {
@Id
@GeneratedValue
@Audited
private int id;
@Audited
@Column(name = "ORG_NAME")
private String name;
public Organization() {
}
}
}