微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Spring JPA多个持久化单元:注入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 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.所以我试图做以下:

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

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

FINAL FIX:
感谢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不是 properly configured.你应该使用一个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配置执行相同操作.

原文地址:https://www.jb51.cc/java/126533.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐