Announcement Announcement Module
Collapse
No announcement yet.
Spring JPA Hibernate JBoss Multiple Persistence Unit Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring JPA Hibernate JBoss Multiple Persistence Unit

    Hi Friends,

    I was looking for an information about how can I configure multiple persistence unit in Spring which is managed by J2EE server(JBoss 5.x) with hibernate as jpa implementation. What I want to do is , declare two Persistence Unit in persistence.xml and deploy ear file to jboss and let it create emf. And in spring look up for those two persitence unit using
    Code:
    <jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/myPersistenceUnit"/>
    so I might need two emf declaration one for PU1 and one for PU2. Now I also want to use datasource exposed by the jboss. At the same time I want spring to resolve dependency in my two base dao classes where i have declared EntityManger with @PersistenceContext(unitName="pu1").
    Can somebody show me proper place where I can look for this configuration. I have successfully done for one, but when i add second I get the exception stating the expected one EMF but found 2.

  • #2
    Hi,

    I'm facing similar problem. I resolved part of it configuring the beans with xml.
    Note that you can't use the same class (e.g.: MyDAO.class) to access two different PersistenceUnit if you configure the PersistenceUnit using @PersistenceContext, because you must specify the unit name in the annotation (on compile-time). If you don't specify the unit name, Spring Container will try to automatic inject one PersistenceUnit, but it will find two PU defined. So, it can't guess which one you are looking for. You MUST tell him which one to use. The only way I found to tell this is configuring the beans through xml.

    Here is my configuration:

    Code:
    <!-- The EMFs-->
    <jee:jndi-lookup jndi-name="jpa/PersistenceUnitFactory-01" id="entityManagerFactory-01"
        	expected-type="javax.persistence.EntityManagerFactory"
        />
    <jee:jndi-lookup jndi-name="jpa/PersistenceUnitFactory-02" id="entityManagerFactory-02"
        	expected-type="javax.persistence.EntityManagerFactory"
        />
    
    <!-- The EMs -->
    <bean class="org.springframework.orm.jpa.support.SharedEntityManagerBean"
    	id="entityManager-01">
    	<property name="entityManagerFactory" ref="entityManagerFactory-01" />
    </bean>
    <bean class="org.springframework.orm.jpa.support.SharedEntityManagerBean"
    	id="entityManager-02">
    	<property name="entityManagerFactory" ref="entityManagerFactory-02" />
    </bean>
    
    <!-- DAOs -->
    <bean class="com.MyDAO" id="myDAO01">
    	<property name="entityManager" ref="entityManager-01"/>
    </bean>
    <bean class="com.MyDAO" id="myDAO02">
    	<property name="entityManager" ref="entityManager-02"/>
    </bean>
    And the persistence.xml
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    	<persistence-unit name="PersistenceUnit-01" transaction-type="JTA">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>dataSource-01</jta-data-source>
            <properties>
            	<property name="jboss.entity.manager.jndi.name" value="jpa/PersistenceUnit-01"/>
    		<property name="jboss.entity.manager.factory.jndi.name" value="jpa/PersistenceUnitFactory-01"/>
                    <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
            </properties>
        </persistence-unit>
        <persistence-unit name="PersistenceUnit-02" transaction-type="JTA">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>dataSource-02</jta-data-source>
            <properties>
            	<property name="jboss.entity.manager.jndi.name" value="jpa/PersistenceUnit-02"/>
    		<property name="jboss.entity.manager.factory.jndi.name" value="jpa/PersistenceUnitFactory-02"/>
                    <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
            </properties>
        </persistence-unit>
    </persistence>
    Hope this help you...

    Comment


    • #3
      Hi camilo, could u help me?

      I'm trying to do the same thing, but it's not work correctly.

      My spring-servlet.xml

      Code:
      <?xml  version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:aop="http://www.springframework.org/schema/aop"
          xmlns:context="http://www.springframework.org/schema/context"
          xmlns:jee="http://www.springframework.org/schema/jee"
          xmlns:lang="http://www.springframework.org/schema/lang"
          xmlns:p="http://www.springframework.org/schema/p"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:util="http://www.springframework.org/schema/util"
          xmlns:mvc="http://www.springframework.org/schema/mvc"
          xsi:schemaLocation="
              http://www.springframework.org/schema/beans 
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/aop 
              http://www.springframework.org/schema/aop/spring-aop.xsd
              http://www.springframework.org/schema/context 
              http://www.springframework.org/schema/context/spring-context.xsd
              http://www.springframework.org/schema/jee 
              http://www.springframework.org/schema/jee/spring-jee.xsd
              http://www.springframework.org/schema/lang 
              http://www.springframework.org/schema/lang/spring-lang.xsd
              http://www.springframework.org/schema/tx 
              http://www.springframework.org/schema/tx/spring-tx.xsd
              http://www.springframework.org/schema/util 
              http://www.springframework.org/schema/util/spring-util.xsd
              http://www.springframework.org/schema/mvc
              http://www.springframework.org/schema/mvc/spring-mvc.xsd">
      
            <jee:jndi-lookup id="entityManagerFactory" jndi-name="java:/CorePUFactory" expected-type="javax.persistence.EntityManagerFactory"/>
      	  <jee:jndi-lookup id="entityManagerFactoryLog" jndi-name="java:/LogPUFactory" expected-type="javax.persistence.EntityManagerFactory"/>    
      
            <context:annotation-config />
          
            <bean id="transactionManager-01" class="org.hibernate.transaction.JBossTransactionManagerLookup">
                  <property name="transactionManager" ref="entityManagerFactory"/>
            </bean>
          
            <bean id="transactionManager-02" class="org.hibernate.transaction.JBossTransactionManagerLookup">
                  <property name="transactionManager" ref="entityManagerFactoryLog"/>
            </bean>    
          
            <tx:annotation-driven transaction-manager="transactionManager"/>
      
      
          <mvc:annotation-driven />
          
          <context:component-scan base-package="com" />
          
          <mvc:resources mapping="/resources/**" location="/resources/" />    
          
          <bean id="jspViewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
              <property name="prefix" value="/WEB-INF/jsp/" />
              <property name="suffix" value=".jsp" />
          </bean>
          
          <bean id="messageSource"
              class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
              <property name="basename" value="classpath:messages" />
              <property name="defaultEncoding" value="UTF-8" />
          </bean>
          
          <bean id="multipartResolver"
              class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
          </bean>
          
          
          ....
          
          
      </beans>
      And my persistence.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="Core" transaction-type="JTA">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>java:/CoreDS</jta-data-source>
            
      		...
            	
            	       	 
            <properties>
      		<property name="jboss.entity.manager.jndi.name" value="java:/Core"/>		
      		<property name="jboss.entity.manager.factory.jndi.name" value="java:/CorePUFactory" />
               <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
               <property name="hibernate.show_sql" value="false"/>
               <!-- <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/> -->
               <property name="transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
             	 <property name="transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
            </properties>
         </persistence-unit>
         
         <persistence-unit name="Log" transaction-type="JTA">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>java:/LogDS</jta-data-source>
            
      		<class>com.xxx.vo.IntegrationLog</class>
            	 
            <properties>
      		<property name="jboss.entity.manager.jndi.name" value="java:/Log"/>		
      		<property name="jboss.entity.manager.factory.jndi.name" value="java:/LogPUFactory" />
               <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
               <property name="hibernate.show_sql" value="false"/>
               <property name="transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
             	 <property name="transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
            </properties>
         </persistence-unit>
         
         
      </persistence>

      Comment


      • #4
        Hello,

        What kind of error are you getting? Please tell more details (stacktrace..) so we can help you..

        Originally posted by rtassini View Post
        Hi camilo, could u help me?

        I'm trying to do the same thing, but it's not work correctly.

        My spring-servlet.xml

        Code:
        <?xml  version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:aop="http://www.springframework.org/schema/aop"
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:jee="http://www.springframework.org/schema/jee"
            xmlns:lang="http://www.springframework.org/schema/lang"
            xmlns:p="http://www.springframework.org/schema/p"
            xmlns:tx="http://www.springframework.org/schema/tx"
            xmlns:util="http://www.springframework.org/schema/util"
            xmlns:mvc="http://www.springframework.org/schema/mvc"
            xsi:schemaLocation="
                http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/aop 
                http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/jee 
                http://www.springframework.org/schema/jee/spring-jee.xsd
                http://www.springframework.org/schema/lang 
                http://www.springframework.org/schema/lang/spring-lang.xsd
                http://www.springframework.org/schema/tx 
                http://www.springframework.org/schema/tx/spring-tx.xsd
                http://www.springframework.org/schema/util 
                http://www.springframework.org/schema/util/spring-util.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        
              <jee:jndi-lookup id="entityManagerFactory" jndi-name="java:/CorePUFactory" expected-type="javax.persistence.EntityManagerFactory"/>
        	  <jee:jndi-lookup id="entityManagerFactoryLog" jndi-name="java:/LogPUFactory" expected-type="javax.persistence.EntityManagerFactory"/>    
        
              <context:annotation-config />
            
              <bean id="transactionManager-01" class="org.hibernate.transaction.JBossTransactionManagerLookup">
                    <property name="transactionManager" ref="entityManagerFactory"/>
              </bean>
            
              <bean id="transactionManager-02" class="org.hibernate.transaction.JBossTransactionManagerLookup">
                    <property name="transactionManager" ref="entityManagerFactoryLog"/>
              </bean>    
            
              <tx:annotation-driven transaction-manager="transactionManager"/>
        
        
            <mvc:annotation-driven />
            
            <context:component-scan base-package="com" />
            
            <mvc:resources mapping="/resources/**" location="/resources/" />    
            
            <bean id="jspViewResolver"
                class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                <property name="prefix" value="/WEB-INF/jsp/" />
                <property name="suffix" value=".jsp" />
            </bean>
            
            <bean id="messageSource"
                class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
                <property name="basename" value="classpath:messages" />
                <property name="defaultEncoding" value="UTF-8" />
            </bean>
            
            <bean id="multipartResolver"
                class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            </bean>
            
            
            ....
            
            
        </beans>
        And my persistence.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="Core" transaction-type="JTA">
              <provider>org.hibernate.ejb.HibernatePersistence</provider>
              <jta-data-source>java:/CoreDS</jta-data-source>
              
        		...
              	
              	       	 
              <properties>
        		<property name="jboss.entity.manager.jndi.name" value="java:/Core"/>		
        		<property name="jboss.entity.manager.factory.jndi.name" value="java:/CorePUFactory" />
                 <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
                 <property name="hibernate.show_sql" value="false"/>
                 <!-- <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/> -->
                 <property name="transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
               	 <property name="transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
              </properties>
           </persistence-unit>
           
           <persistence-unit name="Log" transaction-type="JTA">
              <provider>org.hibernate.ejb.HibernatePersistence</provider>
              <jta-data-source>java:/LogDS</jta-data-source>
              
        		<class>com.xxx.vo.IntegrationLog</class>
              	 
              <properties>
        		<property name="jboss.entity.manager.jndi.name" value="java:/Log"/>		
        		<property name="jboss.entity.manager.factory.jndi.name" value="java:/LogPUFactory" />
                 <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
                 <property name="hibernate.show_sql" value="false"/>
                 <property name="transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
               	 <property name="transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
              </properties>
           </persistence-unit>
           
           
        </persistence>

        Comment

        Working...
        X