gpt4 book ai didi

java - 没有 persistence.xml 的多个entityManagerFactory

转载 作者:行者123 更新时间:2023-12-02 11:20:17 26 4
gpt4 key购买 nike

我一直在一个使用 spring mvc 4 的项目中工作,并且 JPA 配置 bean 是在 java 类中创建的,没有 persistence.xml,就像下面的代码一样。

package test.conf;

import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
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.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class JPAConfiguration {

@Bean
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

factoryBean.setJpaVendorAdapter(vendorAdapter);
factoryBean.setDataSource(dataSource());

Properties props = new Properties();
props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
props.setProperty("hibernate.show_sql", "false");
props.setProperty("hibernate.hbm2ddl.auto", "update");

factoryBean.setJpaProperties(props);
String [] packages = {"test.model"};
factoryBean.setPackagesToScan(packages);

return factoryBean;
}

@Bean
@Primary
public DriverManagerDataSource dataSource() {

DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUsername("root");
dataSource.setPassword("root");
dataSource.setUrl("jdbc:mysql://localhost:3306/database");
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
return dataSource;
}

@Bean
@Primary
public JpaTransactionManager transactionManager(EntityManagerFactory emf){
return new JpaTransactionManager(emf);
}

}

这工作正常,但现在,我们必须连接到第二个数据库 (ORACLE),并且需要第二个实体管理器工厂。我怎样才能做到这一点 ?我找到了在 persistence.xml 上创建第二个持久性单元的示例,但我在该项目中没有 persistence.xml。其他人教如何使用 Spring Boot 创建与多个数据源的连接,但这不是 Spring Boot 项目。

我尝试为另一个数据库创建第二个JPAConfiguration类,然后为每个entitymanagerfactorybean指定一个factoryBean.setPersistenceUnitName("");,但Spring似乎搞砸了,因为它总是初始化一个持久性单位两次。

最佳答案

您需要在属性中拥有第二个数据源详细信息

#second db ...
spring.secondDatasource.url = [url]
spring.secondDatasource.username = [username]
spring.secondDatasource.password = [password]
spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver

然后创建配置类,该类将具有使用第二个数据源的所有配置,并使用第二个数据源启用 jpa 存储库。

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "secondEntityManagerFactory",
transactionManagerRef = "secondTransactionManager",
basePackages = {"com.server.second.repo"}
)
public class SecondDBConfiguration

@Bean("secondDatasource")
@ConfigurationProperties(prefix="spring.secondDatasource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "secondEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
secondEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("secondDatasource") DataSource dataSource
) {
return
builder
.dataSource(dataSource)
.packages("com.server.shadow.domain")
.persistenceUnit("shadow")
.build();
}
@Bean(name = "secondTransactionManager")
public PlatformTransactionManager secondTransactionManager(
@Qualifier("secondEntityManagerFactory") EntityManagerFactory
secondEntityManagerFactory
) {
return new JpaTransactionManager(secondEntityManagerFactory);
}

关于java - 没有 persistence.xml 的多个entityManagerFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49970919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com