From: Frank Schimmel Date: Wed, 12 Feb 2014 14:16:18 +0000 (+0100) Subject: Properly support constraints expressed by bean validation (jsr303) annotations. X-Git-Tag: hibernate4-maven-plugin-1.0.4~12 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=1979c6349fc2a9e0fe3f028fa1cc76557b32031c;p=hibernate-maven-plugin Properly support constraints expressed by bean validation (jsr303) annotations. * 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. --- diff --git a/pom.xml b/pom.xml index 21a5b055..010df70a 100644 --- a/pom.xml +++ b/pom.xml @@ -109,8 +109,9 @@ UTF-8 - 4.3.0.Final - 4.3.0.Final + 4.3.1.Final + 4.3.1.Final + 4.3.1.Final 3.0.4 1.0.1 1.0.3 @@ -146,7 +147,7 @@ org.hibernate hibernate-validator - 4.3.1.Final + ${hibernate-validator.version} org.scannotation diff --git a/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java b/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java index ef983c2d..f73fa2b5 100644 --- a/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java +++ b/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java @@ -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); diff --git a/src/main/java/de/juplo/plugins/hibernate4/ValidationConfiguration.java b/src/main/java/de/juplo/plugins/hibernate4/ValidationConfiguration.java index a1592f73..6748ba5e 100644 --- a/src/main/java/de/juplo/plugins/hibernate4/ValidationConfiguration.java +++ b/src/main/java/de/juplo/plugins/hibernate4/ValidationConfiguration.java @@ -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 + * @author Frank Schimmel */ public class ValidationConfiguration extends Configuration { - Class dialectClass; + private static final long serialVersionUID = 1L; + + private Class dialectClass; - public ValidationConfiguration(String dialectClass) - throws - ClassNotFoundException + public ValidationConfiguration(final String dialectClass) { - this.dialectClass = Class.forName(dialectClass); + try { + this.dialectClass = (Class) 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 index 00000000..faece3f9 --- /dev/null +++ b/src/main/java/org/hibernate/cfg/beanvalidation/TypeSafeActivatorAccessor.java @@ -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 + */ +public class TypeSafeActivatorAccessor extends TypeSafeActivator { + // Empty: just makes public methods accessible. +}