👋🏻

I'm now attempting to migrate our project to SB 3.0.0 and everything up until now I managed to work out fine, but now I am hitting some issues with migrating the data source.

Symptoms look like this: for whichever table I try to do a save in, I get this error

SpringBoot 2.7.1 to 3.0.0 (javax to jakarta datasource) issue, multiple schemas

And you might tell me, well yeah, just create the damn sequence, right? Not really. Somehow, spring now treats all tables as sequences and expects them to be named "{{table_name}}_seq" whereas I already have entity tables (not sequences) with the name {{table_name}}.

Moreoever, as stated in the title, our app uses multiple schemas and 2 databases, thus, having two configurations for both of them:

package com.datacave.acteamo.config.db;

import com.datacave.acteamo.persistence.QuerydslJpaRepositoryFactoryBean;
import jakarta.persistence.EntityManagerFactory;
import java.util.HashMap;
import java.util.Map;
***import javax.sql.DataSource;***
import lombok.RequiredArgsConstructor;
import org.hibernate.cfg.Environment;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
@EnableJpaRepositories(
    repositoryFactoryBeanClass = QuerydslJpaRepositoryFactoryBean.class,
    basePackages = "com.datacave.acteamo.tenant",
    entityManagerFactoryRef = "tenantEntityManagerFactory",
    transactionManagerRef = "tenantTransactionManager")
@RequiredArgsConstructor
public class TenantDbConfig {

  private final JpaProperties jpaProperties;

  @Bean
  JpaVendorAdapter jpaVendorAdapter() {
    return new HibernateJpaVendorAdapter();
  }

  @Bean
  @ConfigurationProperties("tenant.datasource")
  public DataSource tenantDataSource() {
    return DataSourceBuilder.create().build();
  }

  @Bean
  public LocalContainerEntityManagerFactoryBean tenantEntityManagerFactory(
      MultiTenantConnectionProvider multiTenantConnectionProviderImpl,
      CurrentTenantIdentifierResolver currentTenantIdentifierResolverImpl) {

    Map<String, Object> jpaPropertiesMap = new HashMap<>(jpaProperties.getProperties());
    jpaPropertiesMap.put(
        Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProviderImpl);
    jpaPropertiesMap.put(
        Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolverImpl);

    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(tenantDataSource());
    em.setPackagesToScan("com.datacave.acteamo.tenant");
    em.setJpaVendorAdapter(this.jpaVendorAdapter());
    em.setJpaPropertyMap(jpaPropertiesMap);
    em.setPersistenceUnitName("tenantDB");
    return em;
  }

  @Bean
  public PlatformTransactionManager tenantTransactionManager(
      @Qualifier("tenantEntityManagerFactory") EntityManagerFactory tenantEntityManagerFactory) {
    return new JpaTransactionManager(tenantEntityManagerFactory);
  }
}

This is the config for the multi-schema db. Notice that I am still using import javax.sql.DataSource; instead of import jakarta.activation.DataSource; I haven't found any documentation that could indicate that this would be required. All entities are indeed marked with import jakarta.persistence.Entity; however.

Did I miss any documentation around this?

Comment From: scottfrederick

If you would like us to spend some time investigating, please provide a complete minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it and attaching it to this issue.

Comment From: alintreznai

@Alex-Cosma Hibernate 6 uses different naming strategies for sequences and by default it expects a sequence to exist for each table as detailed here: https://thorben-janssen.com/sequence-naming-strategies-in-hibernate-6/.

As a workaround you can add this to your jpaPropertiesMap:

jpaPropertiesMap.put(AvailableSettings.ID_DB_STRUCTURE_NAMING_STRATEGY , "legacy");

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.