2 つのデータベース (MySQL と MSSQL) で動作するように Spring+Hibernate+JPA を構成しようとしています。
私の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">
<!--
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 には、次のように 1 つのユニットが含まれています。
<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: クラスパスリソースで定義された名前 'persistenceUnitManager' の Bean の作成中にエラーが発生しました [config/datasource-context.xml]: Bean の初期化に失敗しました。ネストされた例外は org.springframework.beans.TypeMismatchException です: タイプ [java.util.ArrayList] のプロパティ値をプロパティ 'persistenceXmlLocation' の必要なタイプ [java.lang.String] に変換できませんでした。ネストされた例外は java.lang.IllegalArgumentException です: タイプ [java.util.ArrayList] の値をプロパティ 'persistenceXmlLocation' の必要なタイプ [java.lang.String] に変換できません:
リストなしでpersistence.xmlを1つだけ残した場合、すべて正常に機能しますが、2つのユニットが必要です...
また、Spring + Hibernate のコンテキストで 2 つのデータベースを操作するための代替ソリューションを見つけようとしているので、解決策をいただければ幸いです。
に変更した後の新しいエラーpersistenceXmlLocations
:
{classpath:config/persistence.local.xml, classpath:config/persistence.remote.xml} で単一のデフォルト永続化ユニットが定義されていません
アップデート:
persistenceUnitName を追加しました。動作しますが、ユニットが 1 つしかないため、まだ助けが必要です。
アップデート:
構成ファイルを変更しました: 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>
persistence.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>
現在、2 つの 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(置換なし)は機能しません。それが私の最後の問題です。