Announcement Announcement Module
Collapse
No announcement yet.
Problem with JPA (Hibernate) and different DataSources. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with JPA (Hibernate) and different DataSources.

    Hi all.

    First, sorry for my english.

    I'm trying use JPA (Hibernate) with two different data sources.
    Following the documentation, this is my configuration files:

    applicationContext.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:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="
    	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
        
    	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    
    	<bean id="userDao" class="model.dao.UserDaoImpl" />
    	<bean id="postDao" class="model.dao.PostDaoImpl" />
        
    	<!-- DataSource MySQL -->
    	<bean id="dataSourceMySQL"
    		class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close"
    		scope="singleton">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost/database1" />
    		<property name="username" value="username" />
    		<property name="password" value="password" />
    	</bean>
        
    	<!-- DataSource PostgreSQL -->
    	<bean id="dataSourcePgSQL"
    		class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close"
    		scope="singleton">
    		<property name="driverClassName" value="org.postgresql.Driver" />
    		<property name="url" value="jdbc:postgresql://localhost/database2" />
    		<property name="username" value="username" />
    		<property name="password" value="password" />
    	</bean>
        
    	<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    		<property name="persistenceXmlLocations">
    			<list>
    				<value>classpath*:META-INF/persistence-mysql.xml</value>
    				<value>classpath*:META-INF/persistence-pgsql.xml</value>
    			</list>
    		</property>
    		<property name="dataSources">
    			<map>
    				<entry key="dataSourceMySQL" value-ref="dataSourceMySQL"/>
    				<entry key="dataSourcePgSQL" value-ref="dataSourcePgSQL"/>
    			</map>
    		</property>
    		<property name="defaultDataSource" ref="dataSourceMySQL"/>
    	</bean>
    
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceUnitManager" ref="persistenceUnitManager"/>
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    		</property>
    	</bean>
        
    	<bean id="transactionManager"
    		class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
    
    	<tx:annotation-driven transaction-manager="transactionManager" />
    </beans>
    persistence-mysql.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_1_0.xsd"
    	version="1.0">
    	<persistence-unit name="puMySQL">
    		<provider>org.hibernate.ejb.HibernatePersistence</provider>
    		<non-jta-data-source>dataSourceMySQL</non-jta-data-source>
    		<properties>
    			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
    		</properties>
    	</persistence-unit>
    </persistence>
    persistence-pgsql.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_1_0.xsd"
    	version="1.0">
    	<persistence-unit name="puPgSQL">
    		<provider>org.hibernate.ejb.HibernatePersistence</provider>
    		<non-jta-data-source>dataSourcePgSQL</non-jta-data-source>
    		<properties>
    			<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
    		</properties>
    	</persistence-unit>
    </persistence>
    and my generic DAO:
    Code:
    package model.dao;
    
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    
    import org.springframework.stereotype.Repository;
    import org.springframework.transaction.annotation.Transactional;
    
    @Repository
    @Transactional
    public abstract class GenericDaoImpl<T> implements GenericDao<T> {
    	
    	private static final Log logger = LogFactory.getLog(GenericDaoImpl.class);
    	
    	private final Class<T> objectClass;
    	
    	private EntityManager em;
    
    	@PersistenceContext(unitName = "puMySQL")
    	public void setEntityManager(EntityManager em) {
    		this.em = em;
    	}
    	
    	public GenericDaoImpl(final Class<T> objectClass) {
    		this.objectClass = objectClass;
    	}
    	
    	public Class<T> getObjectClass() {
    		return this.objectClass;
    	}
    
    	// other methods
    }
    But, on the container startup, i get this problem:
    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: No single default persistence unit defined in {classpath*:META-INF/persistence-mysql.xml, classpath*:META-INF/persistence-pgsql.xml}
    Caused by: java.lang.IllegalStateException: No single default persistence unit defined in {classpath*:META-INF/persistence-mysql.xml, classpath*:META-INF/persistence-pgsql.xml}
    Someone can help me?
Working...
X