From c01dd40a6566b62e3ae85f6c0024c147cf2d78ff Mon Sep 17 00:00:00 2001 From: Lukasz Antoniak Date: Mon, 19 Nov 2012 19:03:59 +0100 Subject: [PATCH] HHH-5910 - Fix and test --- .../RevisionInfoConfiguration.java | 2 +- .../metadata/AuditMetadataGenerator.java | 4 +- .../metadata/CollectionMetadataGenerator.java | 2 +- .../configuration/metadata/MetadataTools.java | 15 ++++-- .../abstractparent/AbstractEntity.java | 31 ++++++++++++ .../AuditedAbstractParentTest.java | 50 +++++++++++++++++++ .../abstractparent/EffectiveEntity1.java | 26 ++++++++++ 7 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/inheritance/tableperclass/abstractparent/AbstractEntity.java create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/inheritance/tableperclass/abstractparent/AuditedAbstractParentTest.java create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/inheritance/tableperclass/abstractparent/EffectiveEntity1.java diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/RevisionInfoConfiguration.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/RevisionInfoConfiguration.java index 43a98ed50d..14f6c29020 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/RevisionInfoConfiguration.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/RevisionInfoConfiguration.java @@ -93,7 +93,7 @@ public class RevisionInfoConfiguration { private Document generateDefaultRevisionInfoXmlMapping() { Document document = XMLHelper.getDocumentFactory().createDocument(); - Element class_mapping = MetadataTools.createEntity(document, new AuditTableData(null, null, globalCfg.getDefaultSchemaName(), globalCfg.getDefaultCatalogName()), null); + Element class_mapping = MetadataTools.createEntity(document, new AuditTableData(null, null, globalCfg.getDefaultSchemaName(), globalCfg.getDefaultCatalogName()), null, null); class_mapping.addAttribute("name", revisionInfoEntityName); class_mapping.addAttribute("table", "REVINFO"); diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/metadata/AuditMetadataGenerator.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/metadata/AuditMetadataGenerator.java index c2660da98c..c3cb4f3c6b 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/metadata/AuditMetadataGenerator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/metadata/AuditMetadataGenerator.java @@ -362,7 +362,7 @@ public final class AuditMetadataGenerator { PersistentClass pc, EntityXmlMappingData xmlMappingData, AuditTableData auditTableData, IdMappingData idMapper) { Element class_mapping = MetadataTools.createEntity(xmlMappingData.getMainXmlMapping(), auditTableData, - pc.getDiscriminatorValue()); + pc.getDiscriminatorValue(), pc.isAbstract()); ExtendedPropertyMapper propertyMapper = new MultiPropertyMapper(); // Checking if there is a discriminator column @@ -387,7 +387,7 @@ public final class AuditMetadataGenerator { String inheritanceMappingType) { String extendsEntityName = verEntCfg.getAuditEntityName(pc.getSuperclass().getEntityName()); Element class_mapping = MetadataTools.createSubclassEntity(xmlMappingData.getMainXmlMapping(), - inheritanceMappingType, auditTableData, extendsEntityName, pc.getDiscriminatorValue()); + inheritanceMappingType, auditTableData, extendsEntityName, pc.getDiscriminatorValue(), pc.isAbstract()); // The id and revision type is already mapped in the parent diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/metadata/CollectionMetadataGenerator.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/metadata/CollectionMetadataGenerator.java index f90fa5c96a..907212bb82 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/metadata/CollectionMetadataGenerator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/metadata/CollectionMetadataGenerator.java @@ -531,7 +531,7 @@ public final class CollectionMetadataGenerator { String catalog = mainGenerator.getCatalog(propertyAuditingData.getJoinTable().catalog(), propertyValue.getCollectionTable()); Element middleEntityXml = MetadataTools.createEntity(xmlMappingData.newAdditionalMapping(), - new AuditTableData(auditMiddleEntityName, auditMiddleTableName, schema, catalog), null); + new AuditTableData(auditMiddleEntityName, auditMiddleTableName, schema, catalog), null, null); Element middleEntityXmlId = middleEntityXml.addElement("composite-id"); // If there is a where clause on the relation, adding it to the middle entity. diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/metadata/MetadataTools.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/metadata/MetadataTools.java index 6dcab46545..c0e3315335 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/metadata/MetadataTools.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/metadata/MetadataTools.java @@ -157,7 +157,7 @@ public class MetadataTools { } private static Element createEntityCommon(Document document, String type, AuditTableData auditTableData, - String discriminatorValue) { + String discriminatorValue, Boolean isAbstract) { Element hibernate_mapping = document.addElement("hibernate-mapping"); hibernate_mapping.addAttribute("auto-import", "false"); @@ -183,16 +183,21 @@ public class MetadataTools { class_mapping.addAttribute("catalog", auditTableData.getCatalog()); } + if (isAbstract != null) { + class_mapping.addAttribute("abstract", isAbstract.toString()); + } + return class_mapping; } - public static Element createEntity(Document document, AuditTableData auditTableData, String discriminatorValue) { - return createEntityCommon(document, "class", auditTableData, discriminatorValue); + public static Element createEntity(Document document, AuditTableData auditTableData, String discriminatorValue, + Boolean isAbstract) { + return createEntityCommon(document, "class", auditTableData, discriminatorValue, isAbstract); } public static Element createSubclassEntity(Document document, String subclassType, AuditTableData auditTableData, - String extendsEntityName, String discriminatorValue) { - Element class_mapping = createEntityCommon(document, subclassType, auditTableData, discriminatorValue); + String extendsEntityName, String discriminatorValue, Boolean isAbstract) { + Element class_mapping = createEntityCommon(document, subclassType, auditTableData, discriminatorValue, isAbstract); class_mapping.addAttribute("extends", extendsEntityName); diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/inheritance/tableperclass/abstractparent/AbstractEntity.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/inheritance/tableperclass/abstractparent/AbstractEntity.java new file mode 100644 index 0000000000..d4700aac0d --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/inheritance/tableperclass/abstractparent/AbstractEntity.java @@ -0,0 +1,31 @@ +package org.hibernate.envers.test.integration.inheritance.tableperclass.abstractparent; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; + +import org.hibernate.envers.Audited; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +@Entity +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Audited +public abstract class AbstractEntity { + @Id + public Long id; + + @Column + public String commonField; + + public AbstractEntity() { + } + + protected AbstractEntity(Long id, String commonField) { + this.commonField = commonField; + this.id = id; + } +} diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/inheritance/tableperclass/abstractparent/AuditedAbstractParentTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/inheritance/tableperclass/abstractparent/AuditedAbstractParentTest.java new file mode 100644 index 0000000000..73553b5472 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/inheritance/tableperclass/abstractparent/AuditedAbstractParentTest.java @@ -0,0 +1,50 @@ +package org.hibernate.envers.test.integration.inheritance.tableperclass.abstractparent; + +import java.util.Iterator; +import javax.persistence.EntityManager; + +import org.junit.Assert; +import org.junit.Test; + +import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase; +import org.hibernate.envers.test.Priority; +import org.hibernate.mapping.Table; +import org.hibernate.testing.TestForIssue; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +@TestForIssue(jiraKey = "HHH-5910") +public class AuditedAbstractParentTest extends BaseEnversJPAFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { AbstractEntity.class, EffectiveEntity1.class }; + } + + @Test + @Priority(10) + public void initData() { + EntityManager em = getEntityManager(); + + // Revision 1 + em.getTransaction().begin(); + EffectiveEntity1 entity = new EffectiveEntity1( 1L, "commonField", "specificField1" ); + em.persist( entity ); + em.getTransaction().commit(); + + em.close(); + } + + @Test + public void testAbstractTableExistence() { + Iterator tableIterator = getCfg().getTableMappings(); + while ( tableIterator.hasNext() ) { + Table table = tableIterator.next(); + if ( "AbstractEntity_AUD".equals( table.getName() ) ) { + Assert.assertFalse( table.isPhysicalTable() ); + return; + } + } + Assert.fail(); + } +} diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/inheritance/tableperclass/abstractparent/EffectiveEntity1.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/inheritance/tableperclass/abstractparent/EffectiveEntity1.java new file mode 100644 index 0000000000..75a8430d93 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/inheritance/tableperclass/abstractparent/EffectiveEntity1.java @@ -0,0 +1,26 @@ +package org.hibernate.envers.test.integration.inheritance.tableperclass.abstractparent; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; + +import org.hibernate.envers.Audited; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +@Entity +@Table(name = "ENTITY_1") +@Audited +public class EffectiveEntity1 extends AbstractEntity { + @Column + public String specificField1; + + public EffectiveEntity1() { + } + + public EffectiveEntity1(Long id, String commonField, String specificField1) { + super( id, commonField ); + this.specificField1 = specificField1; + } +}