Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple database with Spring+Hibernate+JPA
    primarykey
    data
    text
    <p>I'm trying to configure Spring+Hibernate+JPA for work with two databases (MySQL and MSSQL).</p> <p>My datasource-context.xml: </p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"&gt; &lt;!-- Data Source config --&gt; &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${local.jdbc.driver}" p:url="${local.jdbc.url}" p:username="${local.jdbc.username}" p:password="${local.jdbc.password}"&gt; &lt;/bean&gt; &lt;bean id="dataSourceRemote" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${remote.jdbc.driver}" p:url="${remote.jdbc.url}" p:username="${remote.jdbc.username}" p:password="${remote.jdbc.password}" /&gt; &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entity-manager-factory-ref="entityManagerFactory" /&gt; &lt;!-- JPA config --&gt; &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt; &lt;bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"&gt; &lt;property name="persistenceXmlLocations"&gt; &lt;list value-type="java.lang.String"&gt; &lt;value&gt;classpath*:config/persistence.local.xml&lt;/value&gt; &lt;value&gt;classpath*:config/persistence.remote.xml&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;property name="dataSources"&gt; &lt;map&gt; &lt;entry key="localDataSource" value-ref="dataSource" /&gt; &lt;entry key="remoteDataSource" value-ref="dataSourceRemote" /&gt; &lt;/map&gt; &lt;/property&gt; &lt;property name="defaultDataSource" ref="dataSource" /&gt; &lt;/bean&gt; &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="jpaVendorAdapter"&gt; &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:showSql="true" p:generateDdl="true"&gt; &lt;/bean&gt; &lt;/property&gt; &lt;property name="persistenceUnitManager" ref="persistenceUnitManager" /&gt; &lt;property name="persistenceUnitName" value="localjpa"/&gt; &lt;/bean&gt; &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt; &lt;/beans&gt; </code></pre> <p>Each persistence.xml contains one unit, like this:</p> <pre><code>&lt;persistence-unit name="remote" transaction-type="RESOURCE_LOCAL"&gt; &lt;properties&gt; &lt;property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultNamingStrategy" /&gt; &lt;property name="hibernate.dialect" value="${remote.hibernate.dialect}" /&gt; &lt;property name="hibernate.hbm2ddl.auto" value="${remote.hibernate.hbm2ddl.auto}" /&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; </code></pre> <p>PersistenceUnitManager cause following exception:</p> <blockquote> <p>Cannot resolve reference to bean 'persistenceUnitManager' while setting bean property 'persistenceUnitManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'persistenceUnitManager' defined in class path resource [config/datasource-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.util.ArrayList] to required type [java.lang.String] for property 'persistenceXmlLocation'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.util.ArrayList] to required type [java.lang.String] for property 'persistenceXmlLocation': no matching editors or conversion strategy found</p> </blockquote> <p>If left only one persistence.xml without list, every works fine but I need 2 units...</p> <p>I also try to find alternative solution for work with two databases in Spring+Hibernate context, so I would appreciate any solution.</p> <p>New error after changing to <code>persistenceXmlLocations</code>:</p> <blockquote> <p>No single default persistence unit defined in {classpath:config/persistence.local.xml, classpath:config/persistence.remote.xml}</p> </blockquote> <p>Update: </p> <p>I add persistenceUnitName, it works, but only with one unit, still need help.</p> <p>Update:</p> <p>I changed config files: datasource-context.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"&gt; &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${local.jdbc.driver}" p:url="${local.jdbc.url}" p:username="${local.jdbc.username}" p:password="${local.jdbc.password}"&gt; &lt;/bean&gt; &lt;bean id="dataSourceRemote" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${remote.jdbc.driver}" p:url="${remote.jdbc.url}" p:username="${remote.jdbc.username}" p:password="${remote.jdbc.password}"&gt; &lt;/bean&gt; &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"&gt; &lt;property name="defaultPersistenceUnitName" value="pu1" /&gt; &lt;/bean&gt; &lt;bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"&gt; &lt;property name="persistenceXmlLocation" value="${persistence.xml.location}" /&gt; &lt;property name="defaultDataSource" ref="dataSource" /&gt; &lt;!-- problem --&gt; &lt;property name="dataSources"&gt; &lt;map&gt; &lt;entry key="local" value-ref="dataSource" /&gt; &lt;entry key="remote" value-ref="dataSourceRemote" /&gt; &lt;/map&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="jpaVendorAdapter"&gt; &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:showSql="true" p:generateDdl="true"&gt; &lt;/bean&gt; &lt;/property&gt; &lt;property name="persistenceUnitManager" ref="persistenceUnitManager" /&gt; &lt;property name="persistenceUnitName" value="pu1" /&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;/bean&gt; &lt;bean id="entityManagerFactoryRemote" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="jpaVendorAdapter"&gt; &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:showSql="true" p:generateDdl="true"&gt; &lt;/bean&gt; &lt;/property&gt; &lt;property name="persistenceUnitManager" ref="persistenceUnitManager" /&gt; &lt;property name="persistenceUnitName" value="pu2" /&gt; &lt;property name="dataSource" ref="dataSourceRemote" /&gt; &lt;/bean&gt; &lt;tx:annotation-driven /&gt; &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entity-manager-factory-ref="entityManagerFactory" /&gt; &lt;bean id="transactionManagerRemote" class="org.springframework.orm.jpa.JpaTransactionManager" p:entity-manager-factory-ref="entityManagerFactoryRemote" /&gt; &lt;/beans&gt; </code></pre> <p>persistence.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;persistence-unit name="pu1" transaction-type="RESOURCE_LOCAL"&gt; &lt;properties&gt; &lt;property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultNamingStrategy" /&gt; &lt;property name="hibernate.dialect" value="${local.hibernate.dialect}" /&gt; &lt;property name="hibernate.hbm2ddl.auto" value="${local.hibernate.hbm2ddl.auto}" /&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; &lt;persistence-unit name="pu2" transaction-type="RESOURCE_LOCAL"&gt; &lt;properties&gt; &lt;property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultNamingStrategy" /&gt; &lt;property name="hibernate.dialect" value="${remote.hibernate.dialect}" /&gt; &lt;property name="hibernate.hbm2ddl.auto" value="${remote.hibernate.hbm2ddl.auto}" /&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; &lt;/persistence&gt; </code></pre> <p>Now it builds two entityManagerFactory, but both are for Microsoft SQL Server</p> <pre><code>[main] INFO org.hibernate.ejb.Ejb3Configuration - Processing PersistenceUnitInfo [ name: pu1 ...] [main] INFO org.hibernate.cfg.SettingsFactory - RDBMS: Microsoft SQL Server [main] INFO org.hibernate.ejb.Ejb3Configuration - Processing PersistenceUnitInfo [ name: pu2 ...] [main] INFO org.hibernate.cfg.SettingsFactory - RDBMS: Microsoft SQL Server (but must MySQL) </code></pre> <p>I suggest, that use only dataSource, dataSourceRemote (no substitution) is not worked. That's my last problem.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload