From 7c60f01f2d5e5dd40d4f2edb0ef3339c803a7560 Mon Sep 17 00:00:00 2001 From: Lukasz Antoniak Date: Sat, 3 Mar 2012 10:27:35 +0100 Subject: [PATCH] HHH-7106 - Fix, test and documentation --- .../main/docbook/devguide/en-US/Envers.xml | 8 ++- .../hibernate/tool/EnversSchemaGenerator.java | 47 +++++++++++++++++ .../integration/tools/SchemaExportTest.java | 52 +++++++++++++++++++ 3 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 hibernate-envers/src/main/java/org/hibernate/tool/EnversSchemaGenerator.java create mode 100644 hibernate-envers/src/matrix/java/org/hibernate/envers/test/integration/tools/SchemaExportTest.java diff --git a/documentation/src/main/docbook/devguide/en-US/Envers.xml b/documentation/src/main/docbook/devguide/en-US/Envers.xml index 05e3fb2e81..51c1650fbc 100644 --- a/documentation/src/main/docbook/devguide/en-US/Envers.xml +++ b/documentation/src/main/docbook/devguide/en-US/Envers.xml @@ -34,10 +34,14 @@ - And that's all - you can create, modify and delete the entites as always. If you look at the generated + And that's all - you can create, modify and delete the entities as always. If you look at the generated schema for your entities, or at the data persisted by Hibernate, you will notice that there are no changes. However, for each audited entity, a new table is introduced - entity_table_AUD, - which stores the historical data, whenever you commit a transaction. + which stores the historical data, whenever you commit a transaction. Envers automatically creates audit + tables if hibernate.hbm2ddl.auto option is set to create, + create-drop or update. Otherwise, to export complete database schema + programatically, use org.hibernate.tool.EnversSchemaGenerator. Appropriate DDL + statements can be also generated with Ant task described later in this manual. diff --git a/hibernate-envers/src/main/java/org/hibernate/tool/EnversSchemaGenerator.java b/hibernate-envers/src/main/java/org/hibernate/tool/EnversSchemaGenerator.java new file mode 100644 index 0000000000..42e51460c8 --- /dev/null +++ b/hibernate-envers/src/main/java/org/hibernate/tool/EnversSchemaGenerator.java @@ -0,0 +1,47 @@ +package org.hibernate.tool; + +import org.hibernate.HibernateException; +import org.hibernate.cfg.Configuration; +import org.hibernate.envers.configuration.AuditConfiguration; +import org.hibernate.service.ServiceRegistry; +import org.hibernate.tool.hbm2ddl.SchemaExport; + +import java.sql.Connection; +import java.util.Properties; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +public class EnversSchemaGenerator { + private final SchemaExport export; + + public EnversSchemaGenerator(ServiceRegistry serviceRegistry, Configuration configuration) { + configuration = configureAuditing(configuration); + export = new SchemaExport(serviceRegistry, configuration); + } + + public EnversSchemaGenerator(Configuration configuration) { + configuration = configureAuditing(configuration); + export = new SchemaExport(configuration); + } + + public EnversSchemaGenerator(Configuration configuration, Properties properties) throws HibernateException { + configuration = configureAuditing(configuration); + export = new SchemaExport(configuration, properties); + } + + public EnversSchemaGenerator(Configuration configuration, Connection connection) throws HibernateException { + configuration = configureAuditing(configuration); + export = new SchemaExport(configuration, connection); + } + + public SchemaExport export() { + return export; + } + + private Configuration configureAuditing(Configuration configuration) { + configuration.buildMappings(); + AuditConfiguration.getFor(configuration); + return configuration; + } +} diff --git a/hibernate-envers/src/matrix/java/org/hibernate/envers/test/integration/tools/SchemaExportTest.java b/hibernate-envers/src/matrix/java/org/hibernate/envers/test/integration/tools/SchemaExportTest.java new file mode 100644 index 0000000000..797153aac9 --- /dev/null +++ b/hibernate-envers/src/matrix/java/org/hibernate/envers/test/integration/tools/SchemaExportTest.java @@ -0,0 +1,52 @@ +package org.hibernate.envers.test.integration.tools; + +import org.hibernate.MappingException; +import org.hibernate.Session; +import org.hibernate.cfg.Environment; +import org.hibernate.envers.test.AbstractSessionTest; +import org.hibernate.envers.test.Priority; +import org.hibernate.envers.test.entities.StrTestEntity; +import org.hibernate.testing.TestForIssue; +import org.hibernate.tool.EnversSchemaGenerator; +import org.junit.Assert; +import org.junit.Test; + +import java.net.URISyntaxException; +import java.util.Arrays; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +@TestForIssue(jiraKey = "HHH-7106") +public class SchemaExportTest extends AbstractSessionTest { + private Integer id = null; + + @Override + protected void initMappings() throws MappingException, URISyntaxException { + config.addAnnotatedClass(StrTestEntity.class); + // Disable schema auto generation. + config.setProperty(Environment.HBM2DDL_AUTO, ""); + } + + @Test + @Priority(10) + public void testSchemaCreation() { + // Generate complete schema. + new EnversSchemaGenerator(config).export().create(true, true); + + // Populate database with test data. + Session session = getSession(); + session.getTransaction().begin(); + StrTestEntity entity = new StrTestEntity("data"); + session.save(entity); + session.getTransaction().commit(); + + id = entity.getId(); + } + + @Test + public void testAuditDataRetrieval() { + Assert.assertEquals(Arrays.asList(1), getAuditReader().getRevisions(StrTestEntity.class, id)); + Assert.assertEquals(new StrTestEntity("data", id), getAuditReader().find(StrTestEntity.class, id, 1)); + } +}