user1260109
user1260109

Reputation: 73

How to inject different entity manager factories to the same DAO using spring + JPA

I am trying to inject two different EntityManagerFactory instances into the same DAO. I have a generic DAO in a project, let's call it "Project Base", and I have two different projects, Project A and Project B. The persistent units are defined in Project Base.

My problem is that I want to be able to inject the EntityManagerFactory instances into the DAO instead of using @PersistenceUnit as I have two different databases, one which is used by project A and another by Project B.

I only have the basic CRUD operations in the generic DAO, but Spring gives me the following exception:

Error creating bean with name 'genericHibernateDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2

Please find my configuration below:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
    default-autowire="autodetect">
    <context:component-scan base-package="com.entertainment" />


    <!-- Entity Manager -->
    <bean id="abstractEntityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" abstract="true">
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="dataSource" ref="ADataSource" />
    </bean>


    <bean id="AEntityManagerFactory" parent="abstractEntityManagerFactory">
        <property name="persistenceUnitName" value="A" />
    </bean>

    <bean id="BEntityManagerFactory" parent="abstractEntityManagerFactory">
        <property name="persistenceUnitName" value="B" />
    </bean>


    <!-- Datasource -->
    <bean id="ADataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
        <property name="url"
            value="myUrl" />
        <property name="username" value="username" />
        <property name="password" value="password" />
    </bean>

    <bean id="BDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
        <property name="url"
            value="myUrl" />
        <property name="username" value="username" />
        <property name="password" value="password" />
    </bean>


    <!--  Transaction Manager -->
    <bean id="ATransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>


    <bean id="BTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <!-- Vendor adapter -->
    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="databasePlatform" value="org.hibernate.dialect.Oracle9iDialect" />
        <property name="showSql" value="true" />
        <property name="generateDdl" value="true" />
    </bean>

    <!--  generic DAO -->

    <bean id="genericDAO"
        class="mypackage.GenericHibernateDAO"  abstract="true"/>


</beans>

This file is in Project Base:

    <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 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="A" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
            <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.connection.username" value="username" />
            <property name="hibernate.connection.password" value="password" />
            <property name="hibernate.connection.url" value="myurl" />
            <property name="hibernate.max_fetch_depth" value="3" />
            <property name="hibernate.archive.autodetection" value="class" />
        </properties>
    </persistence-unit>
    <persistence-unit name="B" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
            <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.connection.username" value="username" />
            <property name="hibernate.connection.password" value="password" />
            <property name="hibernate.connection.url" value="myurl" />
            <property name="hibernate.max_fetch_depth" value="3" />
            <property name="hibernate.archive.autodetection" value="class" />
        </properties>
    </persistence-unit>
</persistence>

I have been really trying hard to figure this out. I'd appreciate any help.

Thanks in advance!

Upvotes: 1

Views: 1808

Answers (2)

Koitoer
Koitoer

Reputation: 19533

@PersistenceUnit(unitName="A")
EntityManagerFactory entityMgrFactory;

OR (this will inject EMFactory related to persistenceUnit A)

@Autowired
@Qualified("AEntityManagerFactory")
private EntityManagerFactory AEntityManagerFactory;  

OR

@PersistenceContext(unitName="A")
EntityManager em;

Without unitName always expect to be only one EMFactory and as you have two, spring don't know which pass.

Upvotes: 1

Vincent
Vincent

Reputation: 1035

Have you tried configuring your persistence units using @PersistenceUnit.unitName ?

Upvotes: 0

Related Questions