Reputation: 61
as explained here:
http://devmint.blogspot.com/2013/02/hibernate-schema-export-with-hibernate.html
the validator specific annotations are not recognized by SchemaExport any longer.
How can method "injectBeanValidationConstraintToDdlTranslator" be translated to Hibernate 5.2 ?
The configuration file no longer exists as stated here:
Where did Configuration.generateSchemaCreationScript() go in Hibernate 5
Thanks.
Upvotes: 0
Views: 193
Reputation: 61
I found the following way to achive the evaluation of the validator properties during a schema export for Hibernate 5.1.
Add a new Class CustomMetadataContributor:
package de.test;
import org.hibernate.MappingException;
import org.hibernate.boot.internal.ClassLoaderAccessImpl;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.spi.ClassLoaderAccess;
import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.boot.spi.MetadataContributor;
import org.hibernate.cfg.SecondPass;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.jboss.jandex.IndexView;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Map;
public class CustomMetadataContributor implements MetadataContributor {
public void contribute(InFlightMetadataCollector metadataCollector, IndexView jandexIndex){
metadataCollector.addSecondPass(new SecondPass() {
@Override
public void doSecondPass(final Map persistentClasses) throws MappingException {
try {
Method applyDDL = Class.forName("org.hibernate.cfg.beanvalidation.TypeSafeActivator") //
.getMethod("applyRelationalConstraints", ValidatorFactory.class, Collection.class, Map.class, Dialect.class, ClassLoaderAccess.class);
applyDDL.setAccessible(true);
StandardServiceRegistry serviceRegistry = metadataCollector.getMetadataBuildingOptions().getServiceRegistry();
applyDDL.invoke(null, Validation.buildDefaultValidatorFactory() ,metadataCollector.getEntityBindings(), serviceRegistry.getService( ConfigurationService.class ).getSettings(), serviceRegistry.getService( JdbcServices.class ).getDialect(),new ClassLoaderAccessImpl(serviceRegistry.getService( ClassLoaderService.class )));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
}
}
Moreover you need to add a textfile (without a file extension) in path
META-INF/services/org.hibernate.boot.spi.MetadataContributor
containing
de.test.CustomMetadataContributor
whereas de.test needs to be replaced with your package name.
Upvotes: 0