Announcement Announcement Module
Collapse
No announcement yet.
Mutlitple Datasrouces Spring JPA and hibernate auto creating schema Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mutlitple Datasrouces Spring JPA and hibernate auto creating schema

    I'm trying to setup multiple datasources within my application. I've got it to the point where I can now configure each of my entities (DAOs) with the persistenceUnit to use and to query from the right database. But when the app starts up Hibernate is creating tables for all entities in both data sources. Below is my configuration for multiple datasources.

    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" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>           
                <property name="hibernate.hbm2ddl.auto" value="update"/>
                <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
                <property name="hibernate.connection.charSet" value="UTF-8"/>
            </properties>
        </persistence-unit>
    </persistence>
    persistence2.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="persistenceUnit2" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
                <property name="hibernate.hbm2ddl.auto" value="update"/>
                <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
                <property name="hibernate.connection.charSet" value="UTF-8"/>
            </properties>
        </persistence-unit>
    </persistence>
    applicationContext.xml:
    Code:
    <util:properties id="jdbcProperties" location="classpath:META-INF/spring/jdbc.properties"/>
        
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${jdbc.1.driverClassName}"/>
            <property name="url" value="${jdbc.1.url}"/>
            <property name="username" value="${jdbc.1.username}"/>
            <property name="password" value="${jdbc.1.password}"/>
        </bean>
        
        <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${jdbc.2.driverClassName}"/>
            <property name="url" value="${jdbc.2.url}"/>
            <property name="username" value="${jdbc.2.username}"/>
            <property name="password" value="${jdbc.2.password}"/>
        </bean>
        
        <bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    	  <property name="persistenceXmlLocations">
    	    <list>
    	     <value>classpath*:META-INF/persistence.xml</value>
    	     <value>classpath*:META-INF/persistence2.xml</value>
    	    </list>
    	  </property>
    	  <property name="dataSources">
    	   <map>
    	    <entry key="dataSource" value-ref="dataSource"/>
    	    <entry key="dataSource2" value-ref="dataSource2"/>
    	   </map>
    	  </property>
    	  <!-- if no datasource is specified, use this one -->
    	  <property name="defaultDataSource" ref="dataSource"/>
    	</bean>
    	
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceUnitManager" ref="pum"/>
    		<property name="persistenceUnitName" value="persistenceUnit"/>
    	  	<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
    	</bean>
    	
    	<bean id="entityManagerFactory2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    	  	<property name="persistenceUnitManager" ref="pum"/>
    	  	<property name="persistenceUnitName" value="persistenceUnit2"/>
    	  	<property name="persistenceXmlLocation" value="classpath:META-INF/persistence2.xml"/>
    	</bean>
        
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
        
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <qualifier value="persistenceUnit"/>
            <property name="dataSource" ref="dataSource" />
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
        </bean>
        
        <bean id="transactionManager2" class="org.springframework.orm.jpa.JpaTransactionManager">
            <qualifier value="persistenceUnit2"/>
            <property name="dataSource" ref="dataSource2" />
            <property name="entityManagerFactory" ref="entityManagerFactory2"/>
        </bean>
        
        <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
        <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager2"/>
    DAO 1:
    Code:
    @PersistenceContext(unitName="persistenceUnit")
    transient EntityManager entityManager;
    DAO 2:
    Code:
    @PersistenceContext(unitName="persistenceUnit2")
    transient EntityManager entityManager;
    What's the correct configuration for multiple persistenceUnits and for Hibernate to stop it from auto creating the tables for the 1st datasource in the 2nd datasource and vice versa?

  • #2
    maybe this answers your question:
    <property name="hibernate.hbm2ddl.auto" value="validate"/>

    Comment


    • #3
      Did you find the solution? I'm also interested in....

      Comment

      Working...
      X