One of the simplest ways to validate the DDL schema with Spring and Hibernate



On this article, we’re going to see what’s one of the simplest ways to validate the DDL schema and the JPA entity mappings when utilizing Spring and Hibernate.

I made a decision to write down this text after studying this Tweet:

The hbm2ddl validate technique

As I defined in this text, Hibernate offers a SchemaManagementTool that we are able to use to handle or validate the underlying database schema.

Whereas producing the DDL scripts with this Hibernate software will be helpful solely to find out the DB schema that Hibernate expects for the present JPA entity mappings, it’s finest to make use of an computerized schema administration software, like Flyway, to handle your database schema.

Nevertheless, the Hibernate SchemaManagementTool additionally gives a validate technique that may confirm whether or not the JPA entity mappings are suitable with the underlying database schema.

In Spring, you possibly can activate this technique by way of the spring.jpa.hibernate.ddl-auto software property:


When you present this setting, the validation might be achieved each time you bootstrapped Hibernate.

Nevertheless, since integration exams are supposed to run in isolation, it’s widespread to create and destroy the Hibernate schema for each take a look at execution, and validating the schema for each take a look at execution is simply including an pointless overhead.

We are able to truly measure this overhead like this:

SchemaValidator schemaValidator = getSchemaValidator(sessionFactory);

ultimate ExecutionOptions executionOptions = SchemaManagementToolCoordinator

lengthy startNanos = System.nanoTime();

    "Schema validation took: [{}] ms",
    TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos)

When working this take a look at on Shopizer, which is a non-trivial Spring Boot undertaking with over 100 JPA entities, that is what I get printed within the log:

SchemaValidationTest : Schema validation took: [138] ms

So, if the validation takes 138 milliseconds on each take a look at execution and we have now 1000 exams, then the general overhead of the spring.jpa.hibernate.ddl-auto=validate setting goes to be 2 minutes and 18 seconds.

Not good!

Now, chances are you’ll surprise why you’d even want such a validation when you’re already utilizing Flyway or Liquibase to handle your schema.

Even if you’re utilizing an automatic schema administration software, if you’re utilizing JPA, then you will need to ensure that the entity mappings are suitable with the underlying database schema. In any other case, your software shouldn’t be going to work as anticipated, because the JPA supplier may assume a distinct database schema than the precise one.

So, the aim of this validation test is to make sure that the JPA entity mappings are suitable with the underlying database schema.

One of the simplest ways to validate the DDL schema with Spring and Hibernate

Now that we have now seen that it’s not helpful to validate the schema each time Hibernate bootstraps, let’s see how we are able to disable the worldwide validation.

To disable the worldwide schema validation, we simply must set the spring.jpa.hibernate.ddl-auto property to the worth of none:


Having disabled the worldwide schema validation, we are able to now activate the spring.jpa.hibernate.ddl-auto=validate setting just for a single Spring take a look at like this:

    properties = "spring.jpa.hibernate.ddl-auto=validate"
@AutoConfigureTestDatabase(change = Change.NONE)
public class SchemaValidationTest {

    @Take a look at
    public void testSchemaValidity() {}

This manner, we are able to nonetheless validate the schema, however we do it solely as soon as as a substitute of doing it each time the JPA EntityManagerFactory is bootstrapped.

When you loved this text, I guess you’re going to love my E book and Video Programs as effectively.

Seize the deal! 40% discount.

Seize the deal! 40% discount.

Seize the deal! 40% discount.


Validating the DDL schema with the JPA mappings is an important activity, however you shouldn’t do that each time Spring runs an integration take a look at as a result of the validation overhead will simply add as much as the general time it takes to run your take a look at suite.

By validating the database schema solely as soon as, you get precisely the result you needed from the very starting, however with out paying the additional overhead each time you bootstrap JPA and Hibernate.

Transactions and Concurrency Control eBook


Leave a Reply

Your email address will not be published. Required fields are marked *