How to disable schema validation in Hibernate for certain entities?

前端 未结 2 1505
北海茫月
北海茫月 2021-01-05 07:33

How do I disable schema validation in Hibernate for certain entities (not all)? Some of my entities are using SQL which lead to fail validation so I want to disable validati

相关标签:
2条回答
  • 2021-01-05 08:12

    Quite an old question but I thought this might help.

    Validation can be filtered by providing a custom org.hibernate.tool.schema.spi.SchemaFilterProvider that specifies a org.hibernate.tool.schema.spi.SchemaFilter to be used by validate operations. To use the custom provider (as @tomerz mentioned), the property hibernate.hbm2ddl.schema_filter_provider must be set with the name of the class. For example, if using Hibernate as a JPA provider in the persistence.xml add

    <property name="hibernate.hbm2ddl.schema_filter_provider" value="com.my.package.MySchemaFilterProvider"/>. 
    

    It may be set programmatically as well (see Hibernate Programmatic Configuration)

    The provider:

    package com.my.package;
    
    import org.hibernate.tool.schema.internal.DefaultSchemaFilter;
    import org.hibernate.tool.schema.spi.SchemaFilter;
    import org.hibernate.tool.schema.spi.SchemaFilterProvider;  
    
    public class MySchemaFilterProvider implements SchemaFilterProvider {
    
      @Override
      public SchemaFilter getCreateFilter() {
        return DefaultSchemaFilter.INSTANCE;
      }
    
      @Override
      public SchemaFilter getDropFilter() {
        return DefaultSchemaFilter.INSTANCE;
      }
    
      @Override
      public SchemaFilter getMigrateFilter() {
        return DefaultSchemaFilter.INSTANCE;
      }
    
      @Override
      public SchemaFilter getValidateFilter() {
        return MySchemaFilter.INSTANCE;
      }
    }
    

    The custom filter:

    public class MySchemaFilter implements SchemaFilter {
    
      public static final MySchemaFilter INSTANCE = new MySchemaFilter();
    
      @Override
      public boolean includeNamespace(Namespace namespace) {
        return true;
      }
    
      @Override
      public boolean includeTable(Table table) {
        return !table.getName().contains("the name of the entity to exclude");
      }
    
      @Override
      public boolean includeSequence(Sequence sequence) {
        return true;
      }
    }
    

    By doing that, the DefaultSchemaFilter is used for all the operations on the database schema except the validation of the entities. The validation operations will be filtered by MySchemaFilter.

    0 讨论(0)
  • 2021-01-05 08:19

    not sure which version of hibernate you are using, but it can be done using: hibernate.hbm2ddl.schema_filter_provider property

    Hibernate Configuration

    Used to specify the org.hibernate.tool.schema.spi.SchemaFilterProvider to be used by create, drop, migrate, and validate operations on the database schema. SchemaFilterProvider provides filters that can be used to limit the scope of these operations to specific namespaces, tables and sequences. All objects are included by defau

    0 讨论(0)
提交回复
热议问题