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