使用 Spring 时如何注入多个 JPA EntityManager(持久性单元)
How to inject multiple JPA EntityManager (persistence units) when using Spring
我需要一个数据库用于查询(非修改),一个用于命令(修改)。我正在使用 Spring Data JPA,所以我有两个配置 类:
@Configuration
@EnableJpaRepositories(value = "com.company.read",
entityManagerFactoryRef = "readingEntityManagerFactory",
transactionManagerRef = "readingTransactionManager")
@EnableTransactionManagement
public class SpringDataJpaReadingConfiguration {
@Bean(name = "readingEntityManagerFactory")
public EntityManagerFactory readingEntityManagerFactory() {
return Persistence.createEntityManagerFactory("persistence.reading");
}
@Bean(name = "readingExceptionTranslator")
public HibernateExceptionTranslator readingHibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}
@Bean(name = "readingTransactionManager")
public JpaTransactionManager readingTransactionManager() {
return new JpaTransactionManager();
}
}
@Configuration
@EnableJpaRepositories(value = "com.company.write",
entityManagerFactoryRef = "writingEntityManagerFactory",
transactionManagerRef = "writingTransactionManager")
@EnableTransactionManagement
public class SpringDataJpaWritingConfiguration {
@Bean(name = "writingEntityManagerFactory")
public EntityManagerFactory writingEntityManagerFactory() {
return Persistence.createEntityManagerFactory("persistence.writing");
}
@Bean(name = "writingExceptionTranslator")
public HibernateExceptionTranslator writingHibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}
@Bean(name = "writingTransactionManager")
public JpaTransactionManager writingTransactionManager() {
return new JpaTransactionManager();
}
}
在我的存储库中,我有时需要像这样使用 EntityManager 来决定:
@Repository
public class UserReadingRepository {
@PersistenceContext(unitName = "persistence.reading")
private EntityManager em;
// some useful queries here
}
我正在使用 persistence.xml:
中定义的持久性单元名称
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="persistence.reading" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>ReadingDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
<persistence-unit name="persistence.writing" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>WritingDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
Spring 抛出 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'persistence.reading'
已定义。奇怪的是,看起来 Spring 试图用持久性单元名称实例化 一个 bean?我是不是配置有误?
UPDATE:当我从@PersistenceContext 注释中删除unitName = "persistence.reading"
时,我将得到以下错误:
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: readingEntityManagerFactory,writingEntityManagerFactory
更新 2:Rohit 建议(在评论中)改为连接 EntityManagerFactory
。所以我尝试执行以下操作:
@PersistenceUnit(unitName = "persistence.reading")
private EntityManagerFactory emf;
但 Spring 仅报告:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'persistence.reading' is defined
最终修复:
感谢 Vlad 的回答,我能够更新代码以使用以下内容(只需确保您也定义了 dataSource
bean):
@Bean(name = "readingEntityManagerFactory")
public EntityManagerFactory readingEntityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setPersistenceUnitName("persistence.reading");
em.setDataSource(dataSource());
em.setPackagesToScan("com.company");
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
em.afterPropertiesSet();
return em.getObject();
}
EntityManageFactory
配置不正确。您应该改用 LocalContainerEntityManagerFactoryBean
:
@Bean(name = "readingEntityManagerFactory")
public EntityManagerFactory readingEntityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setPersistenceUnitName("persistence.reading");
em.setDataSource(dataSource());
em.setPackagesToScan("com.company");
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
em.afterPropertiesSet();
return em.getObject();
}
此外 JpaTransactionManager
也配置错误。它应该是这样的:
@Bean(name = "readingTransactionManager")
public PlatformTransactionManager readingTransactionManager(){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(readingEntityManagerFactory());
return transactionManager;
}
您需要对读取和写入 EntityManager 配置执行相同的操作。
我需要一个数据库用于查询(非修改),一个用于命令(修改)。我正在使用 Spring Data JPA,所以我有两个配置 类:
@Configuration
@EnableJpaRepositories(value = "com.company.read",
entityManagerFactoryRef = "readingEntityManagerFactory",
transactionManagerRef = "readingTransactionManager")
@EnableTransactionManagement
public class SpringDataJpaReadingConfiguration {
@Bean(name = "readingEntityManagerFactory")
public EntityManagerFactory readingEntityManagerFactory() {
return Persistence.createEntityManagerFactory("persistence.reading");
}
@Bean(name = "readingExceptionTranslator")
public HibernateExceptionTranslator readingHibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}
@Bean(name = "readingTransactionManager")
public JpaTransactionManager readingTransactionManager() {
return new JpaTransactionManager();
}
}
@Configuration
@EnableJpaRepositories(value = "com.company.write",
entityManagerFactoryRef = "writingEntityManagerFactory",
transactionManagerRef = "writingTransactionManager")
@EnableTransactionManagement
public class SpringDataJpaWritingConfiguration {
@Bean(name = "writingEntityManagerFactory")
public EntityManagerFactory writingEntityManagerFactory() {
return Persistence.createEntityManagerFactory("persistence.writing");
}
@Bean(name = "writingExceptionTranslator")
public HibernateExceptionTranslator writingHibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}
@Bean(name = "writingTransactionManager")
public JpaTransactionManager writingTransactionManager() {
return new JpaTransactionManager();
}
}
在我的存储库中,我有时需要像这样使用 EntityManager 来决定:
@Repository
public class UserReadingRepository {
@PersistenceContext(unitName = "persistence.reading")
private EntityManager em;
// some useful queries here
}
我正在使用 persistence.xml:
中定义的持久性单元名称<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="persistence.reading" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>ReadingDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
<persistence-unit name="persistence.writing" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>WritingDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
Spring 抛出 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'persistence.reading'
已定义。奇怪的是,看起来 Spring 试图用持久性单元名称实例化 一个 bean?我是不是配置有误?
UPDATE:当我从@PersistenceContext 注释中删除unitName = "persistence.reading"
时,我将得到以下错误:
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: readingEntityManagerFactory,writingEntityManagerFactory
更新 2:Rohit 建议(在评论中)改为连接 EntityManagerFactory
。所以我尝试执行以下操作:
@PersistenceUnit(unitName = "persistence.reading")
private EntityManagerFactory emf;
但 Spring 仅报告:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'persistence.reading' is defined
最终修复:
感谢 Vlad 的回答,我能够更新代码以使用以下内容(只需确保您也定义了 dataSource
bean):
@Bean(name = "readingEntityManagerFactory")
public EntityManagerFactory readingEntityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setPersistenceUnitName("persistence.reading");
em.setDataSource(dataSource());
em.setPackagesToScan("com.company");
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
em.afterPropertiesSet();
return em.getObject();
}
EntityManageFactory
配置不正确。您应该改用 LocalContainerEntityManagerFactoryBean
:
@Bean(name = "readingEntityManagerFactory")
public EntityManagerFactory readingEntityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setPersistenceUnitName("persistence.reading");
em.setDataSource(dataSource());
em.setPackagesToScan("com.company");
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
em.afterPropertiesSet();
return em.getObject();
}
此外 JpaTransactionManager
也配置错误。它应该是这样的:
@Bean(name = "readingTransactionManager")
public PlatformTransactionManager readingTransactionManager(){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(readingEntityManagerFactory());
return transactionManager;
}
您需要对读取和写入 EntityManager 配置执行相同的操作。