From ac49a906e8566f29767927a0dd8dc6b9328115ab Mon Sep 17 00:00:00 2001 From: Lukasz Antoniak Date: Sat, 16 Apr 2011 18:37:01 +0200 Subject: [PATCH] HHH-5276 - REVINFO in different database schema creation --- .../RevisionInfoConfiguration.java | 12 ++-- .../reventity/DifferentDBSchemaTest.java | 68 +++++++++++++++++++ 2 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/reventity/DifferentDBSchemaTest.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 ba646ca3a0..843a793a4e 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 @@ -72,10 +72,10 @@ public class RevisionInfoConfiguration { revisionPropType = "integer"; } - private Document generateDefaultRevisionInfoXmlMapping() { + private Document generateDefaultRevisionInfoXmlMapping(Configuration cfg) { Document document = DocumentHelper.createDocument(); - Element class_mapping = MetadataTools.createEntity(document, new AuditTableData(null, null, null, null), null); + Element class_mapping = MetadataTools.createEntity(document, new AuditTableData(null, null, cfg.getProperty("org.hibernate.envers.default_schema"), cfg.getProperty("org.hibernate.envers.default_catalog")), null); class_mapping.addAttribute("name", revisionInfoEntityName); class_mapping.addAttribute("table", "REVINFO"); @@ -236,7 +236,7 @@ public class RevisionInfoConfiguration { revisionInfoClass = DefaultRevisionEntity.class; revisionInfoGenerator = new DefaultRevisionInfoGenerator(revisionInfoEntityName, revisionInfoClass, RevisionListener.class, revisionInfoTimestampData, isTimestampAsDate()); - revisionInfoXmlMapping = generateDefaultRevisionInfoXmlMapping(); + revisionInfoXmlMapping = generateDefaultRevisionInfoXmlMapping(cfg); } return new RevisionInfoConfigurationResult( @@ -244,10 +244,10 @@ public class RevisionInfoConfiguration { new RevisionInfoQueryCreator(revisionInfoEntityName, revisionInfoIdData.getName(), revisionInfoTimestampData.getName(), isTimestampAsDate()), generateRevisionInfoRelationMapping(), - new RevisionInfoNumberReader(revisionInfoClass, revisionInfoIdData), revisionInfoEntityName, + new RevisionInfoNumberReader(revisionInfoClass, revisionInfoIdData), revisionInfoEntityName, revisionInfoClass, revisionInfoTimestampData); } - + private boolean isTimestampAsDate() { String typename = revisionInfoTimestampType.getName(); return "date".equals(typename) || "time".equals(typename) || "timestamp".equals(typename); @@ -310,5 +310,5 @@ class RevisionInfoConfigurationResult { public PropertyData getRevisionInfoTimestampData() { return revisionInfoTimestampData; } - + } diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/reventity/DifferentDBSchemaTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/reventity/DifferentDBSchemaTest.java new file mode 100644 index 0000000000..f6905ccadd --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/reventity/DifferentDBSchemaTest.java @@ -0,0 +1,68 @@ +package org.hibernate.envers.test.integration.reventity; + +import org.hibernate.cfg.Environment; +import org.hibernate.ejb.Ejb3Configuration; +import org.hibernate.envers.test.AbstractEntityTest; +import org.hibernate.envers.test.Priority; +import org.hibernate.envers.test.entities.StrTestEntity; +import org.hibernate.mapping.Table; +import org.junit.Test; + +import javax.persistence.EntityManager; +import java.util.Arrays; + +/** + * Tests simple auditing process (read and write operations) when REVINFO and audit tables + * exist in a different database schema. + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +public class DifferentDBSchemaTest extends AbstractEntityTest { + private static final String SCHEMA_NAME = "ENVERS_AUDIT"; + private Integer steId = null; + + @Override + public void configure(Ejb3Configuration cfg) { + // Creates new schema after establishing connection + cfg.setProperty(Environment.URL, cfg.getProperties().getProperty(Environment.URL) + ";INIT=CREATE SCHEMA IF NOT EXISTS " + SCHEMA_NAME); + cfg.setProperty("org.hibernate.envers.default_schema", SCHEMA_NAME); + cfg.addAnnotatedClass(StrTestEntity.class); + } + + @Test + @Priority(10) + public void initData() throws InterruptedException { + // Revision 1 + EntityManager em = getEntityManager(); + em.getTransaction().begin(); + StrTestEntity ste = new StrTestEntity("x"); + em.persist(ste); + steId = ste.getId(); + em.getTransaction().commit(); + + // Revision 2 + em.getTransaction().begin(); + ste = em.find(StrTestEntity.class, steId); + ste.setStr("y"); + em.getTransaction().commit(); + } + + @Test + public void testRevinfoSchemaName() { + Table revisionTable = getCfg().getClassMapping("org.hibernate.envers.DefaultRevisionEntity").getTable(); + assert SCHEMA_NAME.equals(revisionTable.getSchema()); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(StrTestEntity.class, steId)); + } + + @Test + public void testHistoryOfId1() { + StrTestEntity ver1 = new StrTestEntity("x", steId); + StrTestEntity ver2 = new StrTestEntity("y", steId); + + assert getAuditReader().find(StrTestEntity.class, steId, 1).equals(ver1); + assert getAuditReader().find(StrTestEntity.class, steId, 2).equals(ver2); + } +}