Announcement Announcement Module
Collapse
No announcement yet.
Spring-Data with 2 entity managers Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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, 12:28 AM.

    Comment


    • #3
      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).

      Comment


      • #4
        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" />

        Comment

        Working...
        X