yura
yura

Reputation: 14645

How to disable schema validation in Hibernate for certain entities?

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 validation for them.

Upvotes: 12

Views: 10594

Answers (3)

tomerz
tomerz

Reputation: 209

I'm not sure which version of Hibernate you are using, but it can be done using the hibernate.hbm2ddl.schema_filter_provider property.

From 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

Upvotes: 2

Alessandro Vinicius
Alessandro Vinicius

Reputation: 11

Starting from the answers by b0gusb and Spring Boot…

In application.yml add:

spring.jpa.properties.hibernate.hbm2ddl.schema_filter_provider=com.my.package.MySchemaFilterProvider

Upvotes: 1

b0gusb
b0gusb

Reputation: 4701

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.

Upvotes: 15

Related Questions