refactor: Directly configure LocalContainerEntityManagerFactoryBean with HibernateJpaVendorAdapter and rename the primary data source bean.
This commit is contained in:
@@ -5,12 +5,12 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
@@ -44,7 +44,7 @@ public class TenantDataSourceConfig implements WebMvcConfigurer {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public TenantRoutingDataSource tenantRoutingDataSource() {
|
||||
public DataSource dataSource() {
|
||||
TenantRoutingDataSource routingDataSource = new TenantRoutingDataSource();
|
||||
|
||||
// Загружаем тенантов из JSON
|
||||
@@ -78,26 +78,28 @@ public class TenantDataSourceConfig implements WebMvcConfigurer {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public DataSource dataSource(TenantRoutingDataSource tenantRoutingDataSource) {
|
||||
return tenantRoutingDataSource;
|
||||
public TenantRoutingDataSource tenantRoutingDataSource(DataSource dataSource) {
|
||||
return (TenantRoutingDataSource) dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
|
||||
DataSource dataSource, EntityManagerFactoryBuilder builder) {
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
|
||||
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource);
|
||||
em.setPackagesToScan("com.magistr.app.model");
|
||||
|
||||
Map<String, String> jpaProps = new HashMap<>();
|
||||
jpaProps.put("hibernate.hbm2ddl.auto", "update");
|
||||
jpaProps.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
|
||||
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
vendorAdapter.setGenerateDdl(true);
|
||||
vendorAdapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQLDialect");
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
||||
|
||||
return builder
|
||||
.dataSource(dataSource)
|
||||
.packages("com.magistr.app.model")
|
||||
.persistenceUnit("default")
|
||||
.properties(jpaProps)
|
||||
.build();
|
||||
Map<String, Object> props = new HashMap<>();
|
||||
props.put("hibernate.hbm2ddl.auto", "update");
|
||||
props.put("hibernate.show_sql", "false");
|
||||
em.setJpaPropertyMap(props);
|
||||
|
||||
return em;
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
Reference in New Issue
Block a user