Announcement Announcement Module
Collapse
No announcement yet.
How can I configure spring data jpa for multiple persistence units Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How can I configure spring data jpa for multiple persistence units

    I need help configuring spring data jpa for multiple persistence units. My persistence.xml file has two persistence units, as follows:

    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="acsrPersistenceUnit"> 
        <persistence-unit name="edispensePersistenceUnit">
    </persistence>
    I tried the following spring configuration, but it doesn't work. I get a "Could not open JPA EntityManager for transaction ... The user must supply a JDBC connection" error when running tests.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    
    	<bean id="acsrEntityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource" ref="acsrDataSource" />
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
    				<property name="database" value="POSTGRESQL" />								
    			</bean>
    		</property>
    		<property name="jpaProperties">
    			<props>
    				<prop key="hibernate.hbm2ddl.auto">update</prop>
    				<prop key="hibernate.show_sql">true</prop>
    				<prop key="hibernate.format_sql">true</prop>
    			</props>
    		</property>
    		<property name="persistenceUnitName" value="acsrPersistenceUnit" />
    	</bean>
    
    	<bean id="acsrTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="acsrEntityManagerFactory" />
    	</bean>
    	
    	<bean id="acsrDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close">
    		<property name="url"
    			value="jdbc:postgresql://...." />
    		<property name="driverClassName" value="org.postgresql.Driver" />
    		<property name="username" value="dbuser" />
    		<property name="password" value="dbpassword" />
    		<property name="initialSize" value="5" />
    	</bean>
    	
    <bean id="edispenseEntityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<!--<property name="dataSource" ref="edispenseDataSource" />-->
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
    				<property name="database" value="POSTGRESQL" />											
    			</bean>
    		</property>
    		<property name="jpaProperties">
    			<props>
    				<prop key="hibernate.hbm2ddl.auto">update</prop>
    				<prop key="hibernate.show_sql">true</prop>
    				<prop key="hibernate.format_sql">true</prop>
    			</props>
    		</property>
    		<property name="persistenceUnitName" value="edispensePersistenceUnit" />
    	</bean>
    
    	<bean id="edispenseTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="edispenseEntityManagerFactory" />
    	</bean>
    
    	<!-- <jdbc:embedded-database id="edispenseDataSource" type="HSQL" />-->
    
    	<bean id="edispenseDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close">
    		<property name="url"
    			value="jdbc:postgresql://..." />
    		<property name="driverClassName" value="org.postgresql.Driver" />
    		<property name="username" value="dbuser" />
    		<property name="password" value="dbpassword" />	
    	</bean>
        
        <jpa:repositories base-package="com.dsi.camel.repository.acsr" 
        	entity-manager-factory-ref="acsrEntityManagerFactory"
        	transaction-manager-ref="acsrTransactionManager"/>
        	
        <jpa:repositories base-package="com.dsi.camel.repository.edispense" 
        	entity-manager-factory-ref="edispenseEntityManagerFactory"
        	transaction-manager-ref="edispenseTransactionManager"/> 
    	
    </beans>
    Here is a sample entity class:

    Code:
    @Entity
    @Table(name="csrstates")
    public class Csrstate implements Serializable {
    
    	@Id
    	private String csrstate;
    
    ...
    And a sample repository class:

    Code:
    public interface CsrstateRepository extends CrudRepository<Csrstate, String>{		
    
    	public List<Csrstate> findByCsrstatefrequency(Csrstatefrequency frequency);
      
    }
    And, finally, a sample test:

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:repository-context.xml")
    @TransactionConfiguration(transactionManager="acsrTransactionManager")
    @Transactional
    public class CsrstateRepositoryService {
    	
    	 @Autowired
    	 CsrstateRepository csrstateRepository;
    	 
    	 @Autowired
    	 CsrstatefrequencyRepository csrstatefrequencyRepository; 
    
    	 @Test
    	 public void testInsertFrequency(){
    		 Csrstatefrequency frequency = new Csrstatefrequency();
    		 frequency.setCsrstatefrequency("REALTIME");
    		 frequency = csrstatefrequencyRepository.save(frequency);
    		 assertEquals(frequency, csrstatefrequencyRepository.findOne(frequency.getCsrstatefrequency()));
    		 
    	 }
    }
    Can anyone suggest a proper configuration for this, as I have tried just about everything conceivable, but cannot get this to work. Actually, I was able to get things to work using an embedded in-memory database, such as HSQL, by replacing the dataSource beans with this:

    Code:
    <jdbc:embedded-database id="acsrDataSource" type="HSQL" />
    <jdbc:embedded-database id="edispenseDataSource" type="HSQL" />
    But I can't get it to work with the dataSource as configured.
Working...
X