1 package de.juplo.plugins.hibernate4;
3 import javax.validation.Validation;
5 import org.hibernate.cfg.Configuration;
6 import org.hibernate.cfg.beanvalidation.TypeSafeActivatorAccessor;
7 import org.hibernate.dialect.Dialect;
8 import org.hibernate.metamodel.source.MappingException;
12 * This integration is usually performed by BeanValidationIntegrator.
13 * Unfortunately, that integration will only be activated upon
14 * initialization of the ServiceRegistry, which initializes
15 * DatasourceConnectionProviderImpl, which looks up the datasource,
16 * which requires a JNDI context ...
17 * We therefore reimplement the relevant parts of BeanValidatorIntegrator.
18 * Since that must occur after secondPassCompile(), which is invoked by
19 * Configuration.generateSchemaCreationScript, which is invoked by
20 * SchemaExport, some fancy subclassing is needed to invoke the integration
22 * @author Mark Robinson <mark@mrobinson.ca>
23 * @author Frank Schimmel <frank.schimmel@cm4all.com>
25 public class ValidationConfiguration extends Configuration
27 private static final long serialVersionUID = 1L;
29 private Class<Dialect> dialectClass;
31 public ValidationConfiguration(final String dialectClass)
34 this.dialectClass = (Class<Dialect>) Class.forName(dialectClass);
35 } catch (ClassNotFoundException e) {
36 throw new RuntimeException(e);
41 protected void secondPassCompile() throws MappingException
43 super.secondPassCompile();
47 TypeSafeActivatorAccessor.applyRelationalConstraints(
48 Validation.buildDefaultValidatorFactory(),
51 dialectClass.newInstance()
56 throw new RuntimeException(e);
60 public String getTypeDefs()
62 return typeDefs.entrySet().toString();