java – Spring JPA多个持久化单元:注入EntityManager

前端之家收集整理的这篇文章主要介绍了java – Spring JPA多个持久化单元:注入EntityManager前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要使用一个数据库进行查询(非修改),另一个用于命令(修改).我使用的是 Spring Data JPA,所以我有两个配置类:
  1. @Configuration
  2. @EnableJpaRepositories(value = "com.company.read",entityManagerFactoryRef = "readingEntityManagerFactory",transactionManagerRef = "readingTransactionManager")
  3. @EnableTransactionManagement
  4. public class SpringDataJpaReadingConfiguration {
  5.  
  6. @Bean(name = "readingEntityManagerFactory")
  7. public EntityManagerFactory readingEntityManagerFactory() {
  8. return Persistence.createEntityManagerFactory("persistence.reading");
  9. }
  10.  
  11. @Bean(name = "readingExceptionTranslator")
  12. public HibernateExceptionTranslator readingHibernateExceptionTranslator() {
  13. return new HibernateExceptionTranslator();
  14. }
  15.  
  16. @Bean(name = "readingTransactionManager")
  17. public JpaTransactionManager readingTransactionManager() {
  18. return new JpaTransactionManager();
  19. }
  20.  
  21. }
  22.  
  23. @Configuration
  24. @EnableJpaRepositories(value = "com.company.write",entityManagerFactoryRef = "writingEntityManagerFactory",transactionManagerRef = "writingTransactionManager")
  25. @EnableTransactionManagement
  26. public class SpringDataJpaWritingConfiguration {
  27.  
  28. @Bean(name = "writingEntityManagerFactory")
  29. public EntityManagerFactory writingEntityManagerFactory() {
  30. return Persistence.createEntityManagerFactory("persistence.writing");
  31. }
  32.  
  33. @Bean(name = "writingExceptionTranslator")
  34. public HibernateExceptionTranslator writingHibernateExceptionTranslator() {
  35. return new HibernateExceptionTranslator();
  36. }
  37.  
  38. @Bean(name = "writingTransactionManager")
  39. public JpaTransactionManager writingTransactionManager() {
  40. return new JpaTransactionManager();
  41. }
  42.  
  43. }

在我的存储库中,我有时需要使用EntityManager来决定如下使用:

  1. @Repository
  2. public class UserReadingRepository {
  3.  
  4. @PersistenceContext(unitName = "persistence.reading")
  5. private EntityManager em;
  6.  
  7. // some useful queries here
  8. }

我使用persistence.xml中定义的持久化单元的名称

  1. <persistence xmlns="http://java.sun.com/xml/ns/persistence"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
  4. version="2.0">
  5.  
  6. <persistence-unit name="persistence.reading" transaction-type="RESOURCE_LOCAL">
  7. <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
  8. <non-jta-data-source>ReadingDS</non-jta-data-source>
  9. <properties>
  10. <property name="hibernate.dialect" value="org.hibernate.dialect.MysqLDialect" />
  11. <property name="hibernate.show_sql" value="true" />
  12. </properties>
  13. </persistence-unit>
  14.  
  15. <persistence-unit name="persistence.writing" transaction-type="RESOURCE_LOCAL">
  16. <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
  17. <non-jta-data-source>WritingDS</non-jta-data-source>
  18. <properties>
  19. <property name="hibernate.dialect" value="org.hibernate.dialect.MysqLDialect" />
  20. <property name="hibernate.show_sql" value="true" />
  21. </properties>
  22. </persistence-unit>
  23.  
  24. </persistence>

Spring throws org.springframework.beans.factory.NoSuchBeanDefinitionException:未定义名为“persistence.reading”的bean.奇怪的是,看起来Spring试图用持久化单元名称来实例化一个bean?我错配了什么吗?

更新:当我从@PersistenceContext注释中删除unitName =“persistence.reading”时,我会得到以下错误
org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义类型为[javax.persistence.EntityManagerFactory]的符合条件的bean:预期的单个匹配bean但是找到2:readingEntityManagerFactory,writingEntityManagerFactory

更新2:Rohit建议(在注释中)连接EntityManagerFactory.所以我试图做以下:

  1. @PersistenceUnit(unitName = "persistence.reading")
  2. private EntityManagerFactory emf;

但是Spring只报告:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为“persistence.reading”的bean

FINAL FIX:
感谢Vlad的答案,我能够更新代码以使用以下内容(只要确保你也定义了你的dataSource bean):

  1. @Bean(name = "readingEntityManagerFactory")
  2. public EntityManagerFactory readingEntityManagerFactory() {
  3. LocalContainerEntityManagerfactorybean em = new LocalContainerEntityManagerfactorybean();
  4. em.setPersistenceUnitName("persistence.reading");
  5. em.setDataSource(dataSource());
  6. em.setPackagesToScan("com.company");
  7. em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
  8. em.afterPropertiesSet();
  9. return em.getObject();
  10. }

解决方法

EntityManageFactory不是 properly configured.你应该使用一个LocalContainerEntityManagerfactorybean
  1. @Bean(name = "readingEntityManagerFactory")
  2. public EntityManagerFactory readingEntityManagerFactory() {
  3. LocalContainerEntityManagerfactorybean em = new LocalContainerEntityManagerfactorybean();
  4. em.setPersistenceUnitName("persistence.reading");
  5. em.setDataSource(dataSource());
  6. em.setPackagesToScan("com.company");
  7. em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
  8. em.afterPropertiesSet();
  9. return em.getObject();
  10. }

JpaTransactionManager也是配置错误的.应该是这样的:

  1. @Bean(name = "readingTransactionManager")
  2. public PlatformTransactionManager readingTransactionManager(){
  3. JpaTransactionManager transactionManager = new JpaTransactionManager();
  4. transactionManager.setEntityManagerFactory(readingEntityManagerFactory());
  5. return transactionManager;
  6. }

您需要对阅读和写入EntityManager配置执行相同操作.

猜你在找的Java相关文章