Spring + JPA +多个持久性单元:注入EntityManager

时间:2022-09-11 16:52:11

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:

我需要一个数据库用于查询(不修改),一个数据库用于命令(修改)。我正在使用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();
    }

}

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

在我的存储库中,我有时需要与EntityManager一起决定如何使用:

@Repository
public class UserReadingRepository {

    @PersistenceContext(unitName = "persistence.reading")
    private EntityManager em;

    // some useful queries here
}

I am using persistence unit's name as defined in my persistence.xml:

我使用的是持久化单元的名称,如我的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 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?

春天把org.springframework.beans.factory。NoSuchBeanDefinitionException:没有bean命名为“持久性”。阅读的定义。奇怪的是,看起来Spring试图用持久性单元名实例化一个bean ?我错误地配置吗?

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

更新:当我删除unitName =“持久性”时。读取“从@PersistenceContext注释中,我将得到以下错误:org.springframework.beans.factory. factory”。nomiquebeandefinitionexception:没有类型的合格bean [javax.persistence]。定义了EntityManagerFactory]:预期的单个匹配bean,但找到了2:readingEntityManagerFactory、writingEntityManagerFactory

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

更新2:Rohit建议(在评论中)改为连线EntityManagerFactory。所以我试着做以下的事:

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

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

但是Spring只报告:org.springframe .beans.factory。NoSuchBeanDefinitionException:没有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):

最终解决方案:多亏了Vlad的解决方案,我能够更新代码来使用以下代码(只要确保您也定义了数据源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();
}

1 个解决方案

#1


6  

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

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();
}

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

同样,JpaTransactionManager也被错误配置了。应该是这样的:

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

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

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

#1


6  

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

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();
}

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

同样,JpaTransactionManager也被错误配置了。应该是这样的:

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

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

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