Adding envers support (contribution from Victor Tatai)
authorVictor Tatai <vtatai@gmail.com>
Mon, 25 Feb 2013 19:23:29 +0000 (16:23 -0300)
committerKai Moritz <kai@juplo.de>
Tue, 10 Sep 2013 22:16:57 +0000 (00:16 +0200)
pom.xml
src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java

diff --git a/pom.xml b/pom.xml
index 20e3541..1e56809 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       <name>Stephen Johnson</name>
       <email>stejohns@redhat.com</email>
     </contributor>
+    <contributor>
+      <name>Victor Tatai</name>
+      <email>vtatai@gmail.com</email>
+    </contributor>
   </contributors>
 
   <distributionManagement>
       <version>${hibernate-core.version}</version>
     </dependency>
     <dependency>
+      <groupId>org.hibernate</groupId>
+      <artifactId>hibernate-envers</artifactId>
+      <version>${hibernate-core.version}</version>
+    </dependency>
+    <dependency>
       <groupId>org.scannotation</groupId>
       <artifactId>scannotation</artifactId>
       <version>${scannotation.version}</version>
index 96848ed..2174765 100644 (file)
@@ -57,6 +57,7 @@ import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.project.MavenProject;
 import org.hibernate.cfg.Configuration;
 import org.hibernate.cfg.NamingStrategy;
+import org.hibernate.envers.configuration.AuditConfiguration;
 import org.hibernate.tool.hbm2ddl.SchemaExport;
 import org.hibernate.tool.hbm2ddl.SchemaExport.Type;
 import org.hibernate.tool.hbm2ddl.Target;
@@ -82,6 +83,7 @@ public class Hbm2DdlMojo extends AbstractMojo
   public final static String PASSWORD = "hibernate.connection.password";
   public final static String DIALECT = "hibernate.dialect";
   public final static String NAMING_STRATEGY="hibernate.ejb.naming_strategy";
+  public final static String ENVERS = "hibernate.envers";
 
   private final static String MD5S = "schema.md5s";
 
@@ -275,6 +277,12 @@ public class Hbm2DdlMojo extends AbstractMojo
    */
   private boolean format;
 
+  /**
+   * Generate envers schema for auditing tables.
+   *
+   * @parameter expression="${hibernate.envers}" default-value="false"
+   */
+  private boolean envers;
 
   @Override
   public void execute()
@@ -530,6 +538,18 @@ public class Hbm2DdlMojo extends AbstractMojo
         getLog().debug("Using the value " + hibernateNamingStrategy);
       properties.setProperty(NAMING_STRATEGY, hibernateNamingStrategy);
     }
+    if (envers)
+    {
+      if (properties.containsKey(ENVERS))
+        getLog().debug(
+            "Overwriting property " +
+            ENVERS + "=" + properties.getProperty(ENVERS) +
+            " with the value " + envers
+          );
+      else
+        getLog().debug("Using the value " + envers);
+      properties.setProperty(ENVERS, Boolean.toString(envers));
+    }
 
     /** The generated SQL varies with the dialect! */
     if (md5s.containsKey(DIALECT))
@@ -550,6 +570,25 @@ public class Hbm2DdlMojo extends AbstractMojo
       md5s.put(DIALECT, properties.getProperty(DIALECT));
     }
 
+    /** The generated SQL varies with the envers-configuration */
+    if (md5s.containsKey(ENVERS))
+    {
+      String envers = properties.getProperty(ENVERS);
+      if (md5s.get(ENVERS).equals(envers))
+        getLog().debug("Envers unchanged.");
+      else
+      {
+        getLog().debug("Envers changed: " + envers);
+        modified = true;
+        md5s.put(ENVERS, envers);
+      }
+    }
+    else
+    {
+      modified = true;
+      md5s.put(ENVERS, properties.getProperty(ENVERS));
+    }
+
     if (properties.isEmpty())
     {
       getLog().error("No properties set!");
@@ -752,6 +791,14 @@ public class Hbm2DdlMojo extends AbstractMojo
        */
       Thread.currentThread().setContextClassLoader(classLoader);
 
+      config.buildMappings();
+
+      if ("true".equals(properties.getProperty(ENVERS)))
+      {
+        getLog().debug("Using envers");
+        AuditConfiguration.getFor(config);
+      }
+
       SchemaExport export = new SchemaExport(config, connection);
       export.setOutputFile(outputFile);
       export.setDelimiter(delimiter);