From 5fba40e135677130cbe0ff3c59f6055228293d92 Mon Sep 17 00:00:00 2001 From: Mark Robinson Date: Fri, 17 Jan 2014 08:53:47 +0100 Subject: [PATCH] Generated schema now corresponds to hibernate validators set on the beans --- pom.xml | 5 ++ .../juplo/plugins/hibernate4/Hbm2DdlMojo.java | 11 +++- .../hibernate4/ValidationConfiguration.java | 65 +++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/juplo/plugins/hibernate4/ValidationConfiguration.java diff --git a/pom.xml b/pom.xml index 02002296..9980a516 100644 --- a/pom.xml +++ b/pom.xml @@ -131,6 +131,11 @@ hibernate-envers ${hibernate-envers.version} + + org.hibernate + hibernate-validator + 4.3.1.Final + org.scannotation scannotation diff --git a/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java b/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java index d17dfd71..ef983c2d 100644 --- a/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java +++ b/src/main/java/de/juplo/plugins/hibernate4/Hbm2DdlMojo.java @@ -670,7 +670,16 @@ public class Hbm2DdlMojo extends AbstractMojo throw new MojoFailureException("Hibernate configuration is missing!"); } - Configuration config = new Configuration(); + Configuration config= null; + try + { + config = new ValidationConfiguration(hibernateDialect); + } + catch (ClassNotFoundException e) + { + e.printStackTrace(); + } + config.setProperties(properties); if ( properties.containsKey(NAMING_STRATEGY)) diff --git a/src/main/java/de/juplo/plugins/hibernate4/ValidationConfiguration.java b/src/main/java/de/juplo/plugins/hibernate4/ValidationConfiguration.java new file mode 100644 index 00000000..a1592f73 --- /dev/null +++ b/src/main/java/de/juplo/plugins/hibernate4/ValidationConfiguration.java @@ -0,0 +1,65 @@ +package de.juplo.plugins.hibernate4; + +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.Properties; +import org.hibernate.cfg.Configuration; +import org.hibernate.dialect.Dialect; +import org.hibernate.metamodel.source.MappingException; + + +/** + * This integration is usually performed by BeanValidationIntegrator. + * Unfortunately, that integration will only be activated upon + * initialization of the ServiceRegistry, which initializes + * DatasourceConnectionProviderImpl, which looks up the datasource, + * which requires a JNDI context ... + * We therefore reimplement the relevant parts of BeanValidatorIntegrator. + * Since that must occur after secondPassCompile(), which is invoked by + * Configuration.generateSchemaCreationScript, which is invoked by + * SchemaExport, some fancy subclassing is needed to invoke the integration + * at the right time. + * @author Mark Robinson + */ +public class ValidationConfiguration extends Configuration +{ + Class dialectClass; + + public ValidationConfiguration(String dialectClass) + throws + ClassNotFoundException + { + this.dialectClass = Class.forName(dialectClass); + } + + @Override + protected void secondPassCompile() throws MappingException + { + super.secondPassCompile(); + + 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, + classes.values(), + getProperties(), + dialectClass.newInstance() + ); + } + catch (Exception e) + { + throw new RuntimeException(e); + } + } +} -- 2.20.1