From dd2bf67ac992a8fc66b4d38896f5425f8463efd9 Mon Sep 17 00:00:00 2001 From: Chris Cranford Date: Thu, 19 Oct 2017 01:51:19 -0400 Subject: [PATCH] HHH-12018 - Revert fix for HHH-11841: Audit query QueryException on maps using entity-types as keys. --- .../metadata/CollectionMetadataGenerator.java | 12 ++++++------ .../metadata/QueryGeneratorBuilder.java | 17 +++++++---------- .../query/AbstractRelationQueryGenerator.java | 19 ++++--------------- .../query/OneAuditEntityQueryGenerator.java | 6 +++--- .../query/OneEntityQueryGenerator.java | 6 +++--- .../query/ThreeEntityQueryGenerator.java | 6 +++--- .../TwoEntityOneAuditedQueryGenerator.java | 8 ++++---- .../query/TwoEntityQueryGenerator.java | 12 ++++++------ .../EntityMapCompositeElementTest.java | 4 ++++ 9 files changed, 40 insertions(+), 50 deletions(-) diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/CollectionMetadataGenerator.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/CollectionMetadataGenerator.java index 443d19315d..2421eb5c34 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/CollectionMetadataGenerator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/CollectionMetadataGenerator.java @@ -76,6 +76,7 @@ import org.hibernate.mapping.Table; import org.hibernate.mapping.Value; import org.hibernate.type.BagType; import org.hibernate.type.ComponentType; +import org.hibernate.type.CustomType; import org.hibernate.type.ListType; import org.hibernate.type.ManyToOneType; import org.hibernate.type.MapType; @@ -448,8 +449,7 @@ public final class CollectionMetadataGenerator { mainGenerator.getAuditStrategy(), referencingIdData, auditMiddleEntityName, - isKeyRevisionTypeInId(), - isElementRevisionTypeInId() + isRevisionTypeInId() ); // Adding the XML mapping for the referencing entity, if the relation isn't inverse. @@ -471,7 +471,7 @@ public final class CollectionMetadataGenerator { queryGeneratorBuilder, referencedPrefix, propertyAuditingData.getJoinTable().inverseJoinColumns(), - !isLobMapElementType() && isMapElementInPrimaryKey() + !isLobMapElementType() ); // ****** @@ -855,9 +855,9 @@ public final class CollectionMetadataGenerator { // Adding the revision type property to the entity xml. mainGenerator.addRevisionType( - isElementRevisionTypeInId() ? middleEntityXmlId : middleEntityXml, + isRevisionTypeInId() ? middleEntityXmlId : middleEntityXml, middleEntityXml, - isElementRevisionTypeInId() + isRevisionTypeInId() ); // All other properties should also be part of the primary key of the middle entity. @@ -1028,7 +1028,7 @@ public final class CollectionMetadataGenerator { * * @return {@code true} if the revision type should be part of the primary key, otherwise {@code false}. */ - private boolean isElementRevisionTypeInId() { + private boolean isRevisionTypeInId() { return isEmbeddableElementType() || isLobMapElementType(); } diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/QueryGeneratorBuilder.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/QueryGeneratorBuilder.java index 2456dd85a2..c965c6d397 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/QueryGeneratorBuilder.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/QueryGeneratorBuilder.java @@ -35,8 +35,7 @@ public final class QueryGeneratorBuilder { private final MiddleIdData referencingIdData; private final String auditMiddleEntityName; private final List idDatas; - private final boolean elementRevisionTypeInId; - private final boolean keyRevisionTypeInId; + private final boolean revisionTypeInId; QueryGeneratorBuilder( GlobalConfiguration globalCfg, @@ -44,15 +43,13 @@ public final class QueryGeneratorBuilder { AuditStrategy auditStrategy, MiddleIdData referencingIdData, String auditMiddleEntityName, - boolean keyRevisionTypeInId, - boolean elementRevisionTypeInId) { + boolean revisionTypeInId) { this.globalCfg = globalCfg; this.verEntCfg = verEntCfg; this.auditStrategy = auditStrategy; this.referencingIdData = referencingIdData; this.auditMiddleEntityName = auditMiddleEntityName; - this.keyRevisionTypeInId = keyRevisionTypeInId; - this.elementRevisionTypeInId = elementRevisionTypeInId; + this.revisionTypeInId = revisionTypeInId; idDatas = new ArrayList<>(); } @@ -65,20 +62,20 @@ public final class QueryGeneratorBuilder { if ( idDatas.size() == 0 ) { return new OneEntityQueryGenerator( verEntCfg, auditStrategy, auditMiddleEntityName, referencingIdData, - elementRevisionTypeInId, componentDatas + revisionTypeInId, componentDatas ); } else if ( idDatas.size() == 1 ) { if ( idDatas.get( 0 ).isAudited() ) { return new TwoEntityQueryGenerator( globalCfg, verEntCfg, auditStrategy, auditMiddleEntityName, referencingIdData, - idDatas.get( 0 ), elementRevisionTypeInId, keyRevisionTypeInId, componentDatas + idDatas.get( 0 ), revisionTypeInId, componentDatas ); } else { return new TwoEntityOneAuditedQueryGenerator( verEntCfg, auditStrategy, auditMiddleEntityName, referencingIdData, - idDatas.get( 0 ), elementRevisionTypeInId, componentDatas + idDatas.get( 0 ), revisionTypeInId, componentDatas ); } } @@ -92,7 +89,7 @@ public final class QueryGeneratorBuilder { return new ThreeEntityQueryGenerator( globalCfg, verEntCfg, auditStrategy, auditMiddleEntityName, referencingIdData, - idDatas.get( 0 ), idDatas.get( 1 ), elementRevisionTypeInId, componentDatas + idDatas.get( 0 ), idDatas.get( 1 ), revisionTypeInId, componentDatas ); } else { diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/AbstractRelationQueryGenerator.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/AbstractRelationQueryGenerator.java index 0ae2651d31..fab7b0d54c 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/AbstractRelationQueryGenerator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/AbstractRelationQueryGenerator.java @@ -29,18 +29,15 @@ import static org.hibernate.envers.internal.entities.mapper.relation.query.Query public abstract class AbstractRelationQueryGenerator implements RelationQueryGenerator { protected final AuditEntitiesConfiguration verEntCfg; protected final MiddleIdData referencingIdData; - protected final boolean keyRevisionTypeInId; - protected final boolean elementRevisionTypeInId; + protected final boolean revisionTypeInId; protected AbstractRelationQueryGenerator( AuditEntitiesConfiguration verEntCfg, MiddleIdData referencingIdData, - boolean keyRevisionTypeInId, - boolean elementRevisionTypeInId) { + boolean revisionTypeInId) { this.verEntCfg = verEntCfg; this.referencingIdData = referencingIdData; - this.keyRevisionTypeInId = keyRevisionTypeInId; - this.elementRevisionTypeInId = elementRevisionTypeInId; + this.revisionTypeInId = revisionTypeInId; } /** @@ -68,14 +65,6 @@ public abstract class AbstractRelationQueryGenerator implements RelationQueryGen return query; } - protected String getElementRevisionTypePath() { - return getRevisionTypePath( this.elementRevisionTypeInId ); - } - - protected String getKeyRevisionTypePath() { - return getRevisionTypePath( this.keyRevisionTypeInId ); - } - protected String queryToString(QueryBuilder query) { return queryToString( query, Collections.emptyMap() ); } @@ -86,7 +75,7 @@ public abstract class AbstractRelationQueryGenerator implements RelationQueryGen return sb.toString(); } - private String getRevisionTypePath(boolean revisionTypeInId) { + protected String getRevisionTypePath() { return revisionTypeInId ? verEntCfg.getOriginalIdPropName() + "." + verEntCfg.getRevisionTypePropName() : verEntCfg.getRevisionTypePropName(); diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/OneAuditEntityQueryGenerator.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/OneAuditEntityQueryGenerator.java index 37dc8046d9..86958a938c 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/OneAuditEntityQueryGenerator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/OneAuditEntityQueryGenerator.java @@ -43,7 +43,7 @@ public final class OneAuditEntityQueryGenerator extends AbstractRelationQueryGen boolean revisionTypeInId, String mappedBy, boolean mappedByKey) { - super( verEntCfg, referencingIdData, revisionTypeInId, revisionTypeInId ); + super( verEntCfg, referencingIdData, revisionTypeInId ); this.mappedBy = mappedBy; @@ -124,7 +124,7 @@ public final class OneAuditEntityQueryGenerator extends AbstractRelationQueryGen true ); // e.revision_type != DEL - rootParameters.addWhereWithNamedParam( getElementRevisionTypePath(), false, "!=", DEL_REVISION_TYPE_PARAMETER ); + rootParameters.addWhereWithNamedParam( getRevisionTypePath(), false, "!=", DEL_REVISION_TYPE_PARAMETER ); } /** @@ -143,7 +143,7 @@ public final class OneAuditEntityQueryGenerator extends AbstractRelationQueryGen // e.revision = :revision removed.addWhereWithNamedParam( verEntCfg.getRevisionNumberPath(), false, "=", REVISION_PARAMETER ); // e.revision_type = DEL - removed.addWhereWithNamedParam( getElementRevisionTypePath(), false, "=", DEL_REVISION_TYPE_PARAMETER ); + removed.addWhereWithNamedParam( getRevisionTypePath(), false, "=", DEL_REVISION_TYPE_PARAMETER ); } @Override diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/OneEntityQueryGenerator.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/OneEntityQueryGenerator.java index 909313eece..71d6950e07 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/OneEntityQueryGenerator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/OneEntityQueryGenerator.java @@ -31,7 +31,7 @@ public final class OneEntityQueryGenerator extends AbstractRelationQueryGenerato AuditEntitiesConfiguration verEntCfg, AuditStrategy auditStrategy, String versionsMiddleEntityName, MiddleIdData referencingIdData, boolean revisionTypeInId, MiddleComponentData... componentData) { - super( verEntCfg, referencingIdData, revisionTypeInId, revisionTypeInId ); + super( verEntCfg, referencingIdData, revisionTypeInId ); /* * The valid query that we need to create: @@ -99,7 +99,7 @@ public final class OneEntityQueryGenerator extends AbstractRelationQueryGenerato originalIdPropertyName, MIDDLE_ENTITY_ALIAS, inclusive, componentData ); // ee.revision_type != DEL - rootParameters.addWhereWithNamedParam( getElementRevisionTypePath(), "!=", DEL_REVISION_TYPE_PARAMETER ); + rootParameters.addWhereWithNamedParam( getRevisionTypePath(), "!=", DEL_REVISION_TYPE_PARAMETER ); } /** @@ -118,7 +118,7 @@ public final class OneEntityQueryGenerator extends AbstractRelationQueryGenerato // ee.revision = :revision removed.addWhereWithNamedParam( verEntCfg.getRevisionNumberPath(), "=", REVISION_PARAMETER ); // ee.revision_type = DEL - removed.addWhereWithNamedParam( getElementRevisionTypePath(), "=", DEL_REVISION_TYPE_PARAMETER ); + removed.addWhereWithNamedParam( getRevisionTypePath(), "=", DEL_REVISION_TYPE_PARAMETER ); } @Override diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/ThreeEntityQueryGenerator.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/ThreeEntityQueryGenerator.java index a7a23f5f8d..fddc05b012 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/ThreeEntityQueryGenerator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/ThreeEntityQueryGenerator.java @@ -38,7 +38,7 @@ public final class ThreeEntityQueryGenerator extends AbstractRelationQueryGenera MiddleIdData referencingIdData, MiddleIdData referencedIdData, MiddleIdData indexIdData, boolean revisionTypeInId, MiddleComponentData... componentData) { - super( verEntCfg, referencingIdData, revisionTypeInId, revisionTypeInId ); + super( verEntCfg, referencingIdData, revisionTypeInId ); /* * The valid query that we need to create: @@ -148,7 +148,7 @@ public final class ThreeEntityQueryGenerator extends AbstractRelationQueryGenera final String revisionPropertyPath = verEntCfg.getRevisionNumberPath(); final String originalIdPropertyName = verEntCfg.getOriginalIdPropName(); final String eeOriginalIdPropertyPath = MIDDLE_ENTITY_ALIAS + "." + originalIdPropertyName; - final String revisionTypePropName = getElementRevisionTypePath(); + final String revisionTypePropName = getRevisionTypePath(); // (selecting e entities at revision :revision) // --> based on auditStrategy (see above) auditStrategy.addEntityAtRevisionRestriction( @@ -219,7 +219,7 @@ public final class ThreeEntityQueryGenerator extends AbstractRelationQueryGenera // Restrictions to match all rows deleted at exactly given revision. final Parameters removed = disjoint.addSubParameters( "and" ); final String revisionPropertyPath = verEntCfg.getRevisionNumberPath(); - final String revisionTypePropName = getElementRevisionTypePath(); + final String revisionTypePropName = getRevisionTypePath(); // Excluding current revision, because we need to match data valid at the previous one. createValidDataRestrictions( globalCfg, auditStrategy, referencedIdData, versionsMiddleEntityName, remQb, valid, false, indexIdData, componentData diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/TwoEntityOneAuditedQueryGenerator.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/TwoEntityOneAuditedQueryGenerator.java index dfb12cafb2..6c2d615b07 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/TwoEntityOneAuditedQueryGenerator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/TwoEntityOneAuditedQueryGenerator.java @@ -31,9 +31,9 @@ public final class TwoEntityOneAuditedQueryGenerator extends AbstractRelationQue public TwoEntityOneAuditedQueryGenerator( AuditEntitiesConfiguration verEntCfg, AuditStrategy auditStrategy, String versionsMiddleEntityName, MiddleIdData referencingIdData, - MiddleIdData referencedIdData, boolean elementRevisionTypeInId, + MiddleIdData referencedIdData, boolean revisionTypeInId, MiddleComponentData... componentData) { - super( verEntCfg, referencingIdData, elementRevisionTypeInId, elementRevisionTypeInId ); + super( verEntCfg, referencingIdData, revisionTypeInId ); /* * The valid query that we need to create: @@ -112,7 +112,7 @@ public final class TwoEntityOneAuditedQueryGenerator extends AbstractRelationQue originalIdPropertyName, MIDDLE_ENTITY_ALIAS, true, componentData ); // ee.revision_type != DEL - rootParameters.addWhereWithNamedParam( getElementRevisionTypePath(), "!=", DEL_REVISION_TYPE_PARAMETER ); + rootParameters.addWhereWithNamedParam( getRevisionTypePath(), "!=", DEL_REVISION_TYPE_PARAMETER ); } /** @@ -130,7 +130,7 @@ public final class TwoEntityOneAuditedQueryGenerator extends AbstractRelationQue // ee.revision = :revision removed.addWhereWithNamedParam( verEntCfg.getRevisionNumberPath(), "=", REVISION_PARAMETER ); // ee.revision_type = DEL - removed.addWhereWithNamedParam( getElementRevisionTypePath(), "=", DEL_REVISION_TYPE_PARAMETER ); + removed.addWhereWithNamedParam( getRevisionTypePath(), "=", DEL_REVISION_TYPE_PARAMETER ); } @Override diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/TwoEntityQueryGenerator.java b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/TwoEntityQueryGenerator.java index 5e240b62ea..1ebc3b440c 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/TwoEntityQueryGenerator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/internal/entities/mapper/relation/query/TwoEntityQueryGenerator.java @@ -34,8 +34,8 @@ public final class TwoEntityQueryGenerator extends AbstractRelationQueryGenerato GlobalConfiguration globalCfg, AuditEntitiesConfiguration verEntCfg, AuditStrategy auditStrategy, String versionsMiddleEntityName, MiddleIdData referencingIdData, MiddleIdData referencedIdData, - boolean elementRevisionTypeInId, boolean keyRevisionTypeInId, MiddleComponentData... componentData) { - super( verEntCfg, referencingIdData, keyRevisionTypeInId, elementRevisionTypeInId ); + boolean revisionTypeInId, MiddleComponentData... componentData) { + super( verEntCfg, referencingIdData, revisionTypeInId ); /* * The valid query that we need to create: @@ -120,7 +120,7 @@ public final class TwoEntityQueryGenerator extends AbstractRelationQueryGenerato final String revisionPropertyPath = verEntCfg.getRevisionNumberPath(); final String originalIdPropertyName = verEntCfg.getOriginalIdPropName(); final String eeOriginalIdPropertyPath = MIDDLE_ENTITY_ALIAS + "." + originalIdPropertyName; - final String revisionTypePropName = getElementRevisionTypePath(); + final String revisionTypePropName = getRevisionTypePath(); // (selecting e entities at revision :revision) // --> based on auditStrategy (see above) auditStrategy.addEntityAtRevisionRestriction( @@ -149,7 +149,7 @@ public final class TwoEntityQueryGenerator extends AbstractRelationQueryGenerato rootParameters.addWhereWithNamedParam( revisionTypePropName, "!=", DEL_REVISION_TYPE_PARAMETER ); // e.revision_type != DEL rootParameters.addWhereWithNamedParam( - REFERENCED_ENTITY_ALIAS + "." + getKeyRevisionTypePath(), + REFERENCED_ENTITY_ALIAS + "." + revisionTypePropName, false, "!=", DEL_REVISION_TYPE_PARAMETER @@ -169,7 +169,7 @@ public final class TwoEntityQueryGenerator extends AbstractRelationQueryGenerato // Restrictions to match all rows deleted at exactly given revision. final Parameters removed = disjoint.addSubParameters( "and" ); final String revisionPropertyPath = verEntCfg.getRevisionNumberPath(); - final String revisionTypePropName = getElementRevisionTypePath(); + final String revisionTypePropName = getRevisionTypePath(); // Excluding current revision, because we need to match data valid at the previous one. createValidDataRestrictions( globalCfg, @@ -194,7 +194,7 @@ public final class TwoEntityQueryGenerator extends AbstractRelationQueryGenerato removed.addWhereWithNamedParam( revisionTypePropName, "=", DEL_REVISION_TYPE_PARAMETER ); // e.revision_type = DEL removed.addWhereWithNamedParam( - REFERENCED_ENTITY_ALIAS + "." + getKeyRevisionTypePath(), + REFERENCED_ENTITY_ALIAS + "." + revisionTypePropName, false, "=", DEL_REVISION_TYPE_PARAMETER diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/EntityMapCompositeElementTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/EntityMapCompositeElementTest.java index 63df29f95f..fd7291ef77 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/EntityMapCompositeElementTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/collection/EntityMapCompositeElementTest.java @@ -19,6 +19,8 @@ import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase; import org.hibernate.envers.test.Priority; import org.junit.Test; +import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.transaction.TransactionUtil; import static org.junit.Assert.assertEquals; @@ -27,6 +29,7 @@ import static org.junit.Assert.assertNotNull; /** * @author Chris Cranford */ +@TestForIssue(jiraKey = "HHH-11841") public class EntityMapCompositeElementTest extends BaseEnversJPAFunctionalTestCase { private Category category; @@ -51,6 +54,7 @@ public class EntityMapCompositeElementTest extends BaseEnversJPAFunctionalTestCa } @Test + @FailureExpected(jiraKey = "HHH-11841", message = "Reverted fix in HHH-12018 and will be fixed in HHH-12043") public void testRevisionHistory() { final AuditReader reader = getAuditReader();