我正在尝试配置 Spring+Hibernate+JPA 以使用两个数据库(MySQL 和 MSSQL)。
我的数据源-context.xml:
<?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"
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">
<!--
Data Source config
-->
<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}">
</bean>
<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}" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entity-manager-factory-ref="entityManagerFactory" />
<!--
JPA config
-->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list value-type="java.lang.String">
<value>classpath*:config/persistence.local.xml</value>
<value>classpath*:config/persistence.remote.xml</value>
</list>
</property>
<property name="dataSources">
<map>
<entry key="localDataSource" value-ref="dataSource" />
<entry key="remoteDataSource" value-ref="dataSourceRemote" />
</map>
</property>
<property name="defaultDataSource" ref="dataSource" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:showSql="true" p:generateDdl="true">
</bean>
</property>
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
<property name="persistenceUnitName" value="localjpa"/>
</bean>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>
每个persistence.xml 包含一个单元,如下所示:
<persistence-unit name="remote" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultNamingStrategy" />
<property name="hibernate.dialect" value="${remote.hibernate.dialect}" />
<property name="hibernate.hbm2ddl.auto" value="${remote.hibernate.hbm2ddl.auto}" />
</properties>
</persistence-unit>
PersistenceUnitManager 导致以下异常:
设置 bean 属性“persistenceUnitManager”时无法解析对 bean“persistenceUnitManager”的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [config/datasource-context.xml] 中定义名称为“persistenceUnitManager”的 bean 创建时出错:bean 初始化失败;嵌套异常是 org.springframework.beans.TypeMismatchException:无法将类型 [java.util.ArrayList] 的属性值转换为属性“persistenceXmlLocation”所需的类型 [java.lang.String];嵌套异常是 java.lang.IllegalArgumentException:无法将类型 [java.util.ArrayList] 的值转换为属性“persistenceXmlLocation”所需的类型 [java.lang.String]:
如果只留下一个没有列表的 persistence.xml,每个都可以正常工作,但我需要 2 个单元...
我还尝试找到在 Spring+Hibernate 上下文中使用两个数据库的替代解决方案,因此我将不胜感激。
更改为后的新错误persistenceXmlLocations
:
{classpath:config/persistence.local.xml, classpath:config/persistence.remote.xml} 中没有定义单个默认持久性单元
更新:
我添加了 persistenceUnitName,它可以工作,但只有一个单元,仍然需要帮助。
更新:
我更改了配置文件:datasource-context.xml
<?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"
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">
<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}">
</bean>
<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}">
</bean>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
<property name="defaultPersistenceUnitName" value="pu1" />
</bean>
<bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocation" value="${persistence.xml.location}" />
<property name="defaultDataSource" ref="dataSource" /> <!-- problem -->
<property name="dataSources">
<map>
<entry key="local" value-ref="dataSource" />
<entry key="remote" value-ref="dataSourceRemote" />
</map>
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:showSql="true" p:generateDdl="true">
</bean>
</property>
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
<property name="persistenceUnitName" value="pu1" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="entityManagerFactoryRemote"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:showSql="true" p:generateDdl="true">
</bean>
</property>
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
<property name="persistenceUnitName" value="pu2" />
<property name="dataSource" ref="dataSourceRemote" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entity-manager-factory-ref="entityManagerFactory" />
<bean id="transactionManagerRemote" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entity-manager-factory-ref="entityManagerFactoryRemote" />
</beans>
持久性.xml
<?xml version="1.0" encoding="UTF-8"?>
<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="pu1" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultNamingStrategy" />
<property name="hibernate.dialect" value="${local.hibernate.dialect}" />
<property name="hibernate.hbm2ddl.auto" value="${local.hibernate.hbm2ddl.auto}" />
</properties>
</persistence-unit>
<persistence-unit name="pu2" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultNamingStrategy" />
<property name="hibernate.dialect" value="${remote.hibernate.dialect}" />
<property name="hibernate.hbm2ddl.auto" value="${remote.hibernate.hbm2ddl.auto}" />
</properties>
</persistence-unit>
</persistence>
现在它构建了两个 entityManagerFactory,但都是针对 Microsoft SQL Server
[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)
我建议,只使用 dataSource,dataSourceRemote(无替换)是行不通的。这是我的最后一个问题。