• <bdo id='9K6g8'></bdo><ul id='9K6g8'></ul>

    <i id='9K6g8'><tr id='9K6g8'><dt id='9K6g8'><q id='9K6g8'><span id='9K6g8'><b id='9K6g8'><form id='9K6g8'><ins id='9K6g8'></ins><ul id='9K6g8'></ul><sub id='9K6g8'></sub></form><legend id='9K6g8'></legend><bdo id='9K6g8'><pre id='9K6g8'><center id='9K6g8'></center></pre></bdo></b><th id='9K6g8'></th></span></q></dt></tr></i><div id='9K6g8'><tfoot id='9K6g8'></tfoot><dl id='9K6g8'><fieldset id='9K6g8'></fieldset></dl></div>

    1. <small id='9K6g8'></small><noframes id='9K6g8'>

    2. <tfoot id='9K6g8'></tfoot>

      <legend id='9K6g8'><style id='9K6g8'><dir id='9K6g8'><q id='9K6g8'></q></dir></style></legend>
      1. 使用Spring时如何注入多个JPA EntityManager(持久化单元)

        时间:2024-08-23
            <bdo id='8qL6p'></bdo><ul id='8qL6p'></ul>

            <i id='8qL6p'><tr id='8qL6p'><dt id='8qL6p'><q id='8qL6p'><span id='8qL6p'><b id='8qL6p'><form id='8qL6p'><ins id='8qL6p'></ins><ul id='8qL6p'></ul><sub id='8qL6p'></sub></form><legend id='8qL6p'></legend><bdo id='8qL6p'><pre id='8qL6p'><center id='8qL6p'></center></pre></bdo></b><th id='8qL6p'></th></span></q></dt></tr></i><div id='8qL6p'><tfoot id='8qL6p'></tfoot><dl id='8qL6p'><fieldset id='8qL6p'></fieldset></dl></div>

            <tfoot id='8qL6p'></tfoot>

            1. <small id='8qL6p'></small><noframes id='8qL6p'>

              <legend id='8qL6p'><style id='8qL6p'><dir id='8qL6p'><q id='8qL6p'></q></dir></style></legend>
                <tbody id='8qL6p'></tbody>
                1. 本文介绍了使用Spring时如何注入多个JPA EntityManager(持久化单元)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要使用一个数据库进行查询(非修改)和一个用于命令(修改).我使用的是 Spring Data JPA,所以我有两个配置类:

                  I need to use one database for queries (non-modifying) and one for commands (modifying). I am using Spring Data JPA, so I have two configuration classes:

                  @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 来决定这样使用:

                  In my repository I sometimes need to decide with EntityManager to use like so:

                  @Repository
                  public class UserReadingRepository {
                  
                      @PersistenceContext(unitName = "persistence.reading")
                      private EntityManager em;
                  
                      // some useful queries here
                  }
                  

                  我正在使用 persistence.xml 中定义的持久性单元名称:

                  I am using persistence unit's name as defined in my 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 似乎试图用持久性单元名称实例化 a bean?我是不是配置错了什么?

                  Spring throws org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'persistence.reading' is defined. Oddly, it looks like Spring tries to instantiate a bean with persistence unit name? Did I misconfigure something?

                  UPDATE:当我从 @PersistenceContext 注释中删除 unitName = "persistence.reading" 时,我会收到以下错误:org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义 [javax.persistence.EntityManagerFactory] 类型的合格 bean:预期的单个匹配 bean 但找到了 2:readingEntityManagerFactory,writingEntityManagerFactory

                  UPDATE: When I remove unitName = "persistence.reading" from @PersistenceContext annotation, I will get following error instead: 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.所以我尝试执行以下操作:

                  UPDATE 2: Rohit suggested (in the comment) to wire EntityManagerFactory instead. So I tried to do the following:

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

                  但 Spring 只报告:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'persistence.reading' is defined

                  but Spring only reports: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'persistence.reading' is defined

                  最终修复:感谢 Vlad 的回答,我能够更新代码以使用以下内容(只需确保您也定义了 dataSource bean):

                  FINAL FIX: Thanks to Vlad's answer, I was able to update the code to use the following (just make sure you define your dataSource bean as well):

                  @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 代替:

                  The EntityManageFactory is not properly configured. You should use a LocalContainerEntityManagerFactoryBean instead:

                  @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 也配置错误.它应该是这样的:

                  Also the JpaTransactionManager is miss-configured too. It should be something like:

                  @Bean(name = "readingTransactionManager")
                  public PlatformTransactionManager readingTransactionManager(){
                      JpaTransactionManager transactionManager = new JpaTransactionManager();
                      transactionManager.setEntityManagerFactory(readingEntityManagerFactory());
                      return transactionManager;
                  }
                  

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

                  You need to do the same for both the reading and the writing EntityManager configurations.

                  这篇关于使用Spring时如何注入多个JPA EntityManager(持久化单元)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Spring Data Join 规范 下一篇:PersistenceAnnotationBeanPostProcessor 有什么用处吗?

                  相关文章

                2. <tfoot id='Db9QR'></tfoot>
                  <legend id='Db9QR'><style id='Db9QR'><dir id='Db9QR'><q id='Db9QR'></q></dir></style></legend>

                    1. <small id='Db9QR'></small><noframes id='Db9QR'>

                      <i id='Db9QR'><tr id='Db9QR'><dt id='Db9QR'><q id='Db9QR'><span id='Db9QR'><b id='Db9QR'><form id='Db9QR'><ins id='Db9QR'></ins><ul id='Db9QR'></ul><sub id='Db9QR'></sub></form><legend id='Db9QR'></legend><bdo id='Db9QR'><pre id='Db9QR'><center id='Db9QR'></center></pre></bdo></b><th id='Db9QR'></th></span></q></dt></tr></i><div id='Db9QR'><tfoot id='Db9QR'></tfoot><dl id='Db9QR'><fieldset id='Db9QR'></fieldset></dl></div>

                        <bdo id='Db9QR'></bdo><ul id='Db9QR'></ul>