Announcement Announcement Module
Collapse
No announcement yet.
Dealing with multiple persistence units Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dealing with multiple persistence units

    Hi,
    I'm using Spring 3, JPA, Hibernate 3.6, Tomcat 6, trying to get multiple persistence units to work (need to access both Oracle and SQL server). There's a short mentioning of this topic in Spring document http://static.springsource.org/sprin...rence/orm.html (section 13.5.1.4). Anyone has a complete working example?

    I'm running into various errors. If I have "<persistence version='2.0' ..." in persistence.xml file, I get error: "cvc-complex-type.3.1: Value '2.0' of attribute 'version' of element 'persistence' is not valid with respect to the corresponding attribute use. Attribute 'version' has a fixed value of '1.0'". If I change it to "<persistence version='1.0' ..." I get other errors: If I use <bean id="pum" class="org.springframework.orm.jpa.persistenceunit .DefaultPersistenceUnitManager">, and define two entityManagerFactory, the error is "no entityManagerFactory bean defined" (it seems Spring is expecting it by default).

    TIA

  • #2
    Hello

    Post your source code and complete error stack trace, to get a complete idea

    use code tags

    Comment


    • #3
      persistence.xml:
      <?xml version="1.0" encoding="UTF-8"?>
      <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="mysqlPu" transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.ejb.HibernatePersistence </provider>

      <properties>
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
      ...
      </properties>
      </persistence-unit>

      <persistence-unit name="oraclePu" transaction-type="RESOURCE_LOCAL">
      <properties>
      <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
      ...
      </properties>
      </persistence-unit>
      </persistence>

      applicationContext.xml:
      <!-- note: using DefaultPersistenceUnitManager as suggested in Spring document
      http://static.springsource.org/sprin...rence/orm.html
      -->
      <bean id="pum" class="org.springframework.orm.jpa.persistenceunit .DefaultPersistenceUnitManager">
      <property name="persistenceXmlLocations">
      <list>
      <value>classpath*:META-INF/persistence.xml</value>
      </list>
      </property>
      </bean>
      <bean id="emf1" class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
      <property name="persistenceUnitManager" ref="pum"/>
      <property name="persistenceUnitName" value="mysqlPu" />
      </bean>
      <bean id="emf2" class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
      <property name="persistenceUnitManager" ref="pum"/>
      <property name="persistenceUnitName" value="oraclePu" />
      </bean>
      <context:annotation-config />
      <bean id="tm1" class="org.springframework.orm.jpa.JpaTransactionM anager">
      <property name="entityManagerFactory" ref="emf1" />
      </bean>
      <bean id="tm2" class="org.springframework.orm.jpa.JpaTransactionM anager">
      <property name="entityManagerFactory" ref="emf2" />
      </bean>
      <tx:annotation-driven />

      The error message when starting Tomcat:
      org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.context.annotation.internalPe rsistenceAnnotationProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.transaction.interceptor.Trans actionAttributeSourceAdvisor#0': Cannot create inner bean '(inner bean)' of type [org.springframework.transaction.interceptor.Transa ctionInterceptor] while setting bean property 'transactionInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '(inner bean)#1': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'transactionManager' is defined

      Here it looks like Spring is expecting a bean 'transactionManager' be defined.

      Unable to get this to work, I tried it this way:
      * remove "pum" bean definition
      * separate persistence.xml to two: persistence1.xml and persistence2.xml, and removed the "properties part".
      * added two "datasource" beans in applicationContext.xml as you would normal do.
      * EntityManagerFactory beans are defined slightly different:
      <bean id="emf1" class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
      <property name="dataSource" ref="dataSource1" />
      <property name="persistenceXmlLocation" value="classpath:/META-INF/persistence1.xml"/>
      </bean>
      <bean id="emf2" class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
      <property name="dataSource" ref="dataSource2" />
      <property name="persistenceXmlLocation" value="classpath:/META-INF/persistence2.xml"/>
      </bean>

      And now i get error "cvc-complex-type.3.1: Value '2.0' of attribute 'version' of element 'persistence' is not valid with respect to the corresponding attribute use. Attribute 'version' has a fixed value of '1.0'"

      thanks.

      Comment

      Working...
      X