0

I have an application that uses one database, for now i have this data-access-config.xml configured.

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
    <!-- Instructs Spring to perfrom declarative transaction management on annotated classes -->
    <tx:annotation-driven />
    <!-- Drives transactions using local JPA APIs -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <!-- Creates a EntityManagerFactory for use with the Hibernate JPA provider and a simple in-memory data source populated with test data -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
    </bean>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/database1" />
        <property name="username" value="admin1" />
        <property name="password" value="some_pass" />
    </bean>
</beans>

it connects good, but now i need to configure a second database (in the same server), tried to duplicate the EntityManagerfactory but throws an error, that cannot have two Entities managers at the same time so im confused here. Im using Hibernate+JPA+Spring

Thanks!!!

user1352643
  • 39
  • 2
  • 5

2 Answers2

1

Something like this should work I believe:

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    ...
</bean>

<bean id="emf1" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource1" />
    ...
</bean>

The in the DAO, use

@PersistenceContext(unitName = "emf1")
private EntityManager em;

The above will tell the DAO to use the emf1 instance.

Maybe you forgot to name your second entity manager something different than your first?

Snowy Coder Girl
  • 5,408
  • 10
  • 41
  • 72
  • no i renamed the second entity. this is the message that i get from system: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2 – user1352643 Nov 15 '12 at 15:31
  • The probably you're probably having is that Spring will try to get a bean by it's type, unless you specifically say to get it by it's name. So it's looking for a `EntityManagerFactor` object, but finds 2. So it doesn't know which to use. If you're using annotations, you need to add the name. – Snowy Coder Girl Nov 15 '12 at 16:30
  • This is my complete config file: – user1352643 Nov 15 '12 at 18:24
0

You might need to use a "persistence unit manager" which will help manage your persistence units. See the Spring documentation on multiple persistence units. You will have the 2 data sources, 1 entity manager factory, and 1 persistence unit manager.

The entity manager factor will have a reference to the persistence unit manager (instead of the 2 data sources), and then the persistence unit manager will have a reference to the 2 data sources.

Snowy Coder Girl
  • 5,408
  • 10
  • 41
  • 72
  • in the EMF im doing one reference to the PU. like if i have two PU in persistence.xml do i need to add another one?. my understanding about this, is spring Automatically put this two PU (independant of the database engine). so when in a DAO for example we only reference to one PU (that connects always to the two DS defined). is that right? – user1352643 Nov 19 '12 at 16:08