Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-7106 - Fix, test and documentation
- Loading branch information
1 parent
ba6ad4d
commit 7c60f01
Showing
3 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
hibernate-envers/src/main/java/org/hibernate/tool/EnversSchemaGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...-envers/src/matrix/java/org/hibernate/envers/test/integration/tools/SchemaExportTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |