Search code examples
springspring-boothibernatespring-data-jpaspring-data

Spring Boot 2.7.8 with Data JPA Multitenancy fails with SessionFactory was not configured for multi-tenancy


I'm following this guide and its associated git repo to configure a multitenant Spring Data JPA application.

A sample application can be found here: https://github.com/codependent/spring-data-multidb

To see the error you can run any of the included tests.

It works perfectly for Spring Boot 3.x, however I'm stuck at 2.7.8 and the very same code fails on startup with:

Caused by: org.hibernate.HibernateException: SessionFactory was not configured for multi-tenancy
    at app//org.hibernate.internal.AbstractSharedSessionContract.<init>(AbstractSharedSessionContract.java:167)

I tried adding 'spring.jpa.properties.hibernate.tenant_identifier_resolver=com.example.multidb.TenantIdentifierResolver' at application.properties but it was pointless.

How can I make it work for Spring Boot 2.x?


Solution

  • For Spring Boot 2.7.x, it's necessary to specify the hibernate.multiTenancy strategy. In my case:

        @Override
        public void customize(Map<String, Object> hibernateProperties) {
            hibernateProperties.put(AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER, this);
            hibernateProperties.put(AvailableSettings.MULTI_TENANT, "DATABASE");
        }
    

    Besides I had to update the sql creation script:

    create sequence hibernate_sequence start with 1 increment by 50;
    create table person (id bigint not null, name varchar(255), primary key (id));