Skip to content

Commit

Permalink
HHH-7106 - Fix, test and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-antoniak committed Mar 3, 2012
1 parent ba6ad4d commit 7c60f01
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
8 changes: 6 additions & 2 deletions documentation/src/main/docbook/devguide/en-US/Envers.xml
Expand Up @@ -34,10 +34,14 @@
</important>

<para>
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 - <literal>entity_table_AUD</literal>,
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 <literal>hibernate.hbm2ddl.auto</literal> option is set to <literal>create</literal>,
<literal>create-drop</literal> or <literal>update</literal>. Otherwise, to export complete database schema
programatically, use <literal>org.hibernate.tool.EnversSchemaGenerator</literal>. Appropriate DDL
statements can be also generated with Ant task described later in this manual.
</para>

<para>
Expand Down
@@ -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;
}
}
@@ -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));
}
}

0 comments on commit 7c60f01

Please sign in to comment.