Results 1 to 4 of 4

Thread: Spring-Data with 2 entity managers

  1. #1
    Join Date
    Nov 2008
    Posts
    12

    Default Spring-Data with 2 entity managers

    Hi,

    My application is working with different entities from 2 different DB's (for example, the users are in one DB, the invoices are in another DB).
    I have 2 entity managers in my applicationConfig.xml, defined like this:

    Code:
    <bean id="emf1" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    	<property name="persistenceUnitName" value="persistenceUnit1"/>
    </bean>
    
    <bean id="emf2" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    	<property name="persistenceUnitName" value="persistenceUnit2"/>
    </bean>
    (the persistence units are defined in persistence.xml).

    I will have a bunch of DAO's that will use Spring-Data, each Dao connecting to only one of the 2 databases.
    How can I configure the DAO's so Spring Data knows which entity manager to use when creating the implementation of the DAO ?

    Thanks.

  2. #2
    Join Date
    Feb 2013
    Posts
    7

    Default

    Without spring-data we can configure in the following way:
    Create two persistence xml.For example persistence.xml and two-persistence.xml(note xml name should end with persistence.xml).

    Bean Configuration:

    <bean id="entitymanager1"
    class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
    </bean>
    <bean id="entitymanager2"
    class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:META-INF/two-persistence.xml" />

    </bean>

    In DAO:
    To use database one:
    @PersistenceContext(unitName = "DB1")
    private EntityManager entitymanager1;

    For database two:
    @PersistenceContext(unitName = "DB2")
    EntityManager entitymanager2;

    unitName is unit name of persiutence xml.

    Hope this help you.
    Vaijesh
    Last edited by vaijesh; Mar 2nd, 2013 at 12:28 AM.

  3. #3
    Join Date
    Nov 2008
    Posts
    12

    Default

    Thanks, but I already know how to configure 2 EntityManagers and use them inside DaoImpl classes.

    My question was "how to tell Spring Data which Entity Manager I want it to use ?" considering there's no impl for some Spring Data Dao's (none that I write myself).

  4. #4
    Join Date
    Mar 2013
    Posts
    2

    Default

    Quote Originally Posted by caseta View Post
    Thanks, but I already know how to configure 2 EntityManagers and use them inside DaoImpl classes.

    My question was "how to tell Spring Data which Entity Manager I want it to use ?" considering there's no impl for some Spring Data Dao's (none that I write myself).

    <jpa:repositories base-package="com.yourpackage" entity-manager-factory-ref="yourEmf" transaction-manager-ref="yourTransactionManager" />

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •