Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 801
yura Avatar asked Jun 02 '11 08:06

yura


People also ask

How do I disable schema validation?

​Open the web service for which you wish to disable the XML Schema validation. Open the corresponding Operation Mapping. Click on the mapping properties->Advanced tab. Update the 'XML Schema Validation' property value to 'No Validation' (As stated below).

What is Schema based validation?

An API schema defines which API requests are valid based on several request properties like target endpoint and HTTP method. Schema Validation allows you to check if incoming traffic complies with a previously supplied API schema.

What is Hibernate Validator?

Hibernate Validator allows to express and validate application constraints. The default metadata source are annotations, with the ability to override and extend through the use of XML. It is not tied to a specific application tier or programming model and is available for both server and client application programming.


2 Answers

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.

like image 66
b0gusb Avatar answered Oct 04 '22 07:10

b0gusb


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

like image 38
tomerz Avatar answered Oct 04 '22 08:10

tomerz