Announcement Announcement Module
Collapse
No announcement yet.
Spring Data Jpa with Multiple Datasources Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Data Jpa with Multiple Datasources

    Hi!

    I'm trying to use 2 datasources on my project.

    With one entityManager everything goes ok, but when I add a second one I get this Exception:

    Code:
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2
    I've found a post about the problem. It states that a solution is to declaring an abstract bean:

    Code:
    <bean id="abstractEntityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    		abstract="true" />
    Here is the code of my first entity manager:

    Code:
     <bean id="emf"  parent="abstractEntityManagerFactory">
            <property name="dataSource" ref="dataSource" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
            </property>        
            <property name="packagesToScan" value="it.cpmapave.frigoristi.domain"/>
            <property name="jpaProperties">
                <props>
                    <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                    <prop key="hibernate.hbm2ddl.auto">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                    <prop key="hibernate.max_fetch_depth">3</prop>
                    <prop key="hibernate.jdbc.fetch_size">50</prop>
                    <prop key="hibernate.jdbc.batch_size">10</prop>
                    <prop key="hibernate.show_sql">true</prop>
    
                    <!-- Listeners for Hibernate Envers -->
                  
                    <prop key="hibernate.ejb.event.post-insert">
                        org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener
                    </prop>   
                    <prop key="hibernate.ejb.event.post-update">
                        org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener
                    </prop>
                    <prop key="hibernate.ejb.event.post-delete">
                        org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener
                    </prop>
                    <prop key="hibernate.ejb.event.pre-collection-update">
                        org.hibernate.envers.event.AuditEventListener
                    </prop>
                    <prop key="hibernate.ejb.event.pre-collection-remove">
                        org.hibernate.envers.event.AuditEventListener
                    </prop>
                    <prop key="hibernate.ejb.event.post-collection-recreate">
                        org.hibernate.envers.event.AuditEventListener
                    </prop>
                    
                    <!-- Properties for Hibernate Envers --> 
                    <prop key="org.hibernate.envers.audit_table_suffix">_H</prop>
                    <prop key="org.hibernate.envers.revision_field_name">AUDIT_REVISION</prop>
                    <prop key="org.hibernate.envers.revision_type_field_name">ACTION_TYPE</prop>
                    <prop key="org.hibernate.envers.audit_strategy">org.hibernate.envers.strategy.ValidityAuditStrategy</prop>
                    <prop key="org.hibernate.envers.audit_strategy_validity_end_rev_field_name">AUDIT_REVISION_END</prop>
                    <prop key="org.hibernate.envers.audit_strategy_validity_store_revend_timestamp">true</prop>
                    <prop key="org.hibernate.envers.audit_strategy_validity_revend_timestamp_field_name">AUDIT_REVISION_END_TS</prop>                
                </props>        
            </property>
    And here the transaction manager

    Code:
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="emf"/>
        </bean>
    
        <tx:annotation-driven transaction-manager="transactionManager" />
    ..and the jpa repository:

    Code:
     <jpa:repositories base-package="it.cpmapave.frigoristi.repository"
                          entity-manager-factory-ref="emf"
                          transaction-manager-ref="transactionManager"/>
        
        <jpa:auditing auditor-aware-ref="auditorAwareBean"/>
        
        <bean id="auditorAwareBean" class="it.cpmapave.frigoristi.auditor.AuditorAwareBean"/>
    Without specifying a second Entity Manager everything works.. but when I add these lines:

    Code:
     <bean id="emf2"  parent="abstractEntityManagerFactory">
    ....
    I got the exception.

    I've seen that other users have same issue.. but I can't find a solution.

    Any help would be appreciated!
    Marco
    Last edited by mserioli; Jan 14th, 2013, 05:21 AM.

  • #2
    Solved putting in web.xml:

    Code:
    <filter>
        <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
        <init-param>
            <param-name>persistenceUnitName</param-name>
            <param-value>persistenceUnit</param-value>
        </init-param>
      </filter>

    Comment

    Working...
    X