Hibernate Multitenancy and hbm2ddl.auto

I am currently attempting to build a Springboot application with a multi-tenant database implementation, using the multiple database approach. I’ve managed to get something written that works to correctly route my requests to one of several tenant databases.

In our production environment, the database we’re using is Oracle and we have a proper data migration strategy that uses SQL files to define the database schema for the tenants (the contents of the schema are the same across all of the tenants).

My problem is when trying to unit test. Rather than setup a complete Oracle with all of the tenants, I’m using a set of H2 in-memory databases and using Hibernate’s hbm2ddl to create the schema. Unfortunately, when creating the EntityManager, hbm2ddl is only called on one of my tenants (the default). Which of course means that only that tenant can accept queries, etc. The other tenants fail with various SQL exceptions, as expected.

So does anyone know how I might be able to tell the Hibernate multi-tenant implementation to run hbm2ddl on all of the tenants (either at startup or on first access to a specific tenant)? Any pointers would be greatly appreciated.

You can use schema export with the JPA API jakarta.persistence.Persistence#generateSchema or hbm2ddl, but maybe you should consider a tool like Liquibase for this purpose instead.