Properly support constraints expressed by bean validation (jsr303) annotations.
authorFrank Schimmel <frank@lynix.net>
Wed, 12 Feb 2014 14:16:18 +0000 (15:16 +0100)
committerFrank Schimmel <frank@lynix.net>
Wed, 12 Feb 2014 14:16:18 +0000 (15:16 +0100)
 * Access public method of package-visible TypeSafeActivator class without reflection.
 * Fix arguments to call of TypeSafeActivator.applyRelationalConstraints().
 * Use hibernate version 4.3.1.Final for all components.
 * Minor refactorings in exception handling.

pom.xml
src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java
src/main/java/de/juplo/plugins/hibernate4/ValidationConfiguration.java
src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivatorAccessor.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 21a5b05..010df70 100644 (file)
--- a/pom.xml
+++ b/pom.xml
     <!-- Zeichensatz -->
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <!-- Verwendete Versionen -->
-    <hibernate-core.version>4.3.0.Final</hibernate-core.version>
-    <hibernate-envers.version>4.3.0.Final</hibernate-envers.version>
+    <hibernate-core.version>4.3.1.Final</hibernate-core.version>
+    <hibernate-envers.version>4.3.1.Final</hibernate-envers.version>
+    <hibernate-validator.version>4.3.1.Final</hibernate-validator.version>
     <maven.version>3.0.4</maven.version>
     <maven-plugin-log4j.version>1.0.1</maven-plugin-log4j.version>
     <scannotation.version>1.0.3</scannotation.version>
     <dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-validator</artifactId>
-      <version>4.3.1.Final</version>
+      <version>${hibernate-validator.version}</version>
     </dependency>
     <dependency>
       <groupId>org.scannotation</groupId>
index ef983c2..f73fa2b 100644 (file)
@@ -670,15 +670,7 @@ public class Hbm2DdlMojo extends AbstractMojo
       throw new MojoFailureException("Hibernate configuration is missing!");
     }
 
-    Configuration config= null;
-    try
-    {
-      config = new ValidationConfiguration(hibernateDialect);
-    }
-    catch (ClassNotFoundException e)
-    {
-      e.printStackTrace();
-    }
+    final Configuration config = new ValidationConfiguration(hibernateDialect);
 
     config.setProperties(properties);
 
index a1592f7..6748ba5 100644 (file)
@@ -1,9 +1,9 @@
 package de.juplo.plugins.hibernate4;
 
-import java.lang.reflect.Method;
-import java.util.Collection;
-import java.util.Properties;
+import javax.validation.Validation;
+
 import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.beanvalidation.TypeSafeActivatorAccessor;
 import org.hibernate.dialect.Dialect;
 import org.hibernate.metamodel.source.MappingException;
 
@@ -20,16 +20,21 @@ import org.hibernate.metamodel.source.MappingException;
  * SchemaExport, some fancy subclassing is needed to invoke the integration
  * at the right time.
  * @author Mark Robinson <mark@mrobinson.ca>
+ * @author Frank Schimmel <frank.schimmel@cm4all.com>
  */
 public class ValidationConfiguration extends Configuration
 {
-  Class dialectClass;
+  private static final long serialVersionUID = 1L;
+
+  private Class<Dialect> dialectClass;
 
-  public ValidationConfiguration(String dialectClass)
-      throws
-        ClassNotFoundException
+  public ValidationConfiguration(final String dialectClass)
   {
-    this.dialectClass = Class.forName(dialectClass);
+    try {
+        this.dialectClass = (Class<Dialect>) Class.forName(dialectClass);
+    } catch (ClassNotFoundException e) {
+        throw new RuntimeException(e);
+    }
   }
 
   @Override
@@ -39,19 +44,8 @@ public class ValidationConfiguration extends Configuration
 
     try
     {
-      /** Thank you, hibernate folks, for making this useful class package private ... */
-      Method applyDDL =
-          Class
-            .forName("org.hibernate.cfg.beanvalidation.TypeSafeActivator")
-            .getMethod(
-                "applyRelationalConstraints",
-                Collection.class,
-                Properties.class,
-                Dialect.class
-                );
-      applyDDL.setAccessible(true);
-      applyDDL.invoke(
-          null,
+      TypeSafeActivatorAccessor.applyRelationalConstraints(
+          Validation.buildDefaultValidatorFactory(),
           classes.values(),
           getProperties(),
           dialectClass.newInstance()
diff --git a/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivatorAccessor.java b/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivatorAccessor.java
new file mode 100644 (file)
index 0000000..faece3f
--- /dev/null
@@ -0,0 +1,11 @@
+package org.hibernate.cfg.beanvalidation;
+
+/**
+ * This class enables access to the public methods of {@link TypeSafeActivator}
+ * which itself is visible in the package only.
+ *
+ * @author Frank Schimmel <frank.schimmel@cm4all.com>
+ */
+public class TypeSafeActivatorAccessor extends TypeSafeActivator {
+    // Empty: just makes public methods accessible.
+}