【问题标题】:Standalone schema export Hibernate Validator integration独立模式导出 Hibernate Validator 集成
【发布时间】:2019-02-10 18:42:06
【问题描述】:

在这里解释:

http://devmint.blogspot.com/2013/02/hibernate-schema-export-with-hibernate.html

SchemaExport 不再识别验证器特定的注释。

方法“injectBeanValidationConstraintToDdlTranslator”如何翻译成Hibernate 5.2?

配置文件不再存在,如此处所述:

Where did Configuration.generateSchemaCreationScript() go in Hibernate 5

谢谢。

【问题讨论】:

    标签: hibernate ddl schemaexport


    【解决方案1】:

    我发现以下方法可以在 Hibernate 5.1 的架构导出期间实现对验证器属性的评估。

    添加一个新类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);
                    }
                }
            });
        }
    }
    

    此外,您需要在路径中添加一个文本文件(没有文件扩展名)

    META-INF/services/org.hibernate.boot.spi.MetadataContributor

    包含

    de.test.CustomMetadataContributor
    

    de.test 需要替换为您的包名。

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      • 2016-11-06
      相关资源
      最近更新 更多