Announcement Announcement Module
Collapse
No announcement yet.
Spring / Hibernate JPA with multiple datasources Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring / Hibernate JPA with multiple datasources

    Hello All,

    I have following code and I am trying to inject a DAO specific EntityManager with code below in DAO. Persistence XML and context XML are as follows. If I have single datasource everthing works fine from Junit4 test case.JUNIT test is executed in Hibernate JPA 4.2.0 , Spring 3.2.1, but when I add more datasources I get following error:
    .
    Caused by: org.springframework.beans.factory.BeanCreationExce ption:
    Error creating bean with name 'successorFirmDao': Injection of persistence dependencies failed;
    nested exception is org.springframework.beans.factory.NoUniqueBeanDefi nitionException:
    No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined:
    expected single matching bean but found 2: entityManagerFactoryiaWebDao,entityManagerFactoryi aTrustDao
    I am wondering what is wrong in configuration, I presume Spring does support following scenario . Any help is appreciated

    DAO

    Code:
    @PersistenceContext(unitName="iaWebDao")
    	@Autowired
    	private EntityManager genericEntityManager;
    
    
    	public EntityManager getGenericEntityManager() {
    		return genericEntityManager;
    	}
    applicationcontext.xml

    Code:
    <context:property-placeholder location="/META-INF/test/jdbc.properties"/>
      <context:component-scan base-package="com.jumpIt.ia.service,
    	                                      com.jumpIt.ia.service.impl,
    	                                      com.jumpIt.ia.dao ,
    	                                      com.jumpIt.ia.dao.hibernateJPA" />
        <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSourceiaWebDao">
              <property name="driverClassName" value="${jdbc.driverClassName}"/>
              <property name="url" value="${jdbc.iaweb.url}"/>
              <property name="username" value="${jdbc.iaweb.username}"/>
              <property name="password" value="${jdbc.iaweb.password}"/>
        </bean>
        <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSourceiaTrustDao">
              <property name="driverClassName" value="${jdbc.driverClassName}"/>
              <property name="url" value="${jdbc.iatrust.url}"/>
              <property name="username" value="${jdbc.iatrust.username}"/>
              <property name="password" value="${jdbc.iatrust.password}"/>
        </bean>
        <bean id="jpaVendorAdapter"
          class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="DB2"/>
            <property name="showSql" value="true"/>
        </bean>
        <bean id="entityManagerFactoryiaWebDao"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceXmlLocation" value="/META-INF/persistence-test.xml" />
            <property name="persistenceUnitName" value="iaWebDao"/>
            <property name="dataSource" ref="dataSourceiaWebDao"/>
            <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
            <property name="jpaProperties">
                <props>
                    <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
                </props>
            </property>
        </bean>
       <bean id="entityManagerFactoryiaTrustDao"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceXmlLocation" value="/META-INF/persistence-test.xml" />
            <property name="persistenceUnitName" value="iaTrustDao"/>
            <property name="dataSource" ref="dataSourceiaTrustDao"/>
            <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
            <property name="jpaProperties">
                <props>
                    <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
                </props>
            </property>
        </bean>
        <bean id="transactionManageriaWeb" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactoryiaWebDao"/>
            <qualifier value="txManagerIaweb"/>
        </bean>
        <bean id="transactionManageriaTrust" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactoryiaTrustDao"/>
            <qualifier value="txManagerIatrust"/>
        </bean>
    
        <tx:annotation-driven transaction-manager="transactionManageriaWeb"/>
       <tx:annotation-driven transaction-manager="transactionManageriaTrust"/>
        <context:annotation-config />


    persistence-test.xml

    Code:
    <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="iaWebDao" transaction-type="RESOURCE_LOCAL">
    		<provider>org.hibernate.ejb.HibernatePersistence</provider>
    		<properties>
    			<property name="hibernate.bytecode.use_reflection_optimizer"
    				value="false" />
    			<property name="hibernate.connection.driver_class" value="com.ibm.db2.jcc.DB2Driver" />
    			<property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" />
    		</properties>
    	</persistence-unit>
    	<persistence-unit name="iaTrustDao" transaction-type="RESOURCE_LOCAL">
    		<properties>
    			<property name="hibernate.bytecode.use_reflection_optimizer"
    				value="false" />
    			<property name="hibernate.connection.driver_class" value="com.ibm.db2.jcc.DB2Driver" />
    			<property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" />
    		</properties>
    	</persistence-unit>
    </persistence>
Working...
X