使用 Spring+Hibernate+JPA 的多数据库
我正在尝试配置 Spring+Hibernate+JPA 以使用两个数据库(MySQL 和 MSSQL)。
我的 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 包含一个单元,如下所示:
<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” 豆属性 '持久性单元管理器';嵌套的 例外是 org.springframework.beans.factory.BeanCreationException: 使用名称创建 bean 时出错 'persistenceUnitManager' 定义于 类路径资源 [配置/datasource-context.xml]: bean 初始化失败;嵌套的 例外是 org.springframework.beans.TypeMismatchException: 无法转换属性值 输入 [java.util.ArrayList] 为必填项 为属性输入 [java.lang.String] 'persistenceXmlLocation';嵌套的 例外是 java.lang.IllegalArgumentException: 无法转换类型值 [java.util.ArrayList] 为所需类型 [java.lang.String] 属性 “persistenceXmlLocation”:没有匹配 找到编辑器或转换策略
如果只留下一个 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>
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>
现在它构建了两个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(无替换)不起作用。这是我的最后一个问题。
I'm trying to configure Spring+Hibernate+JPA for work with two databases (MySQL and MSSQL).
My 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>
Each persistence.xml contains one unit, like this:
<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 cause following exception:
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
If left only one persistence.xml without list, every works fine
but I need 2 units...
I also try to find alternative solution for work with two databases in Spring+Hibernate context, so I would appreciate any solution.
New error after changing to persistenceXmlLocations
:
No single default persistence unit defined in {classpath:config/persistence.local.xml, classpath:config/persistence.remote.xml}
Update:
I add persistenceUnitName, it works, but only with one unit, still need help.
Update:
I changed config files:
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>
Now it builds two entityManagerFactory, but both are for 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)
I suggest, that use only dataSource, dataSourceRemote (no substitution) is not worked. That's my last problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要使用persistenceXmlLocations属性(注意复数)而不是persistenceXmlLocation。它是一个字符串数组,因此它将从列表自动转换:
更新(基于编辑)
您的
entityManagerFactory
未指定persistenceUnitName
属性。您必须明确地这样做,因为您定义了多个持久性单元,而entityManagerFactory
必须知道要使用哪一个。You need to use
persistenceXmlLocations
property (note the plural) rather thanpersistenceXmlLocation
. It's a string array, so it'll be auto-converted from list:Update (based on edit)
Your
entityManagerFactory
does not specifypersistenceUnitName
property. You have to do so explicitly because you're defining more than one persistence unit andentityManagerFactory
has to know which one to use.Justin,第 3 点可以用更标准的方式完成,如下所示:
在 Spring 上下文中:
在 Spring 管理的 DAO 中(注意 unitName 属性):
这样,与名为“pu1”的持久性单元相对应的正确实例化的EntityManager将被注入到相应的DAO中。
Justin, number 3 can be done in a more standard way like this:
In your Spring context:
In your Spring-managed DAOs (note the unitName property):
This way a properly intantiated EntityManager corresponding to the persistence unit named "pu1" would be injected in the corresponding DAOs.
如果您遵循本教程,
http://www.javacodegeeks.com/ 2010/05/jboss-42x-spring-3-jpa-hibernate.html 您可以进行以下更改来访问两个不同的数据库:
对于您想要访问第二个数据库的每个 DAO,包括以下内容:
<前><代码>@Autowired
私有EntityManagerFactoryEntityManagerFactoryDB2;
@PostConstruct
公共无效初始化(){
super.setEntityManagerFactory(entityManagerFactoryDB2);
}
仅此而已!
在 Spring 服务类上,像往常一样使用 DAO!
If you follow this tutorial,
http://www.javacodegeeks.com/2010/05/jboss-42x-spring-3-jpa-hibernate.html you can make the following changes to access two different databases:
for every DAO you want to access the second database include the following :
Thats all!
On spring service classes, use the DAOs as usual!
我以这种方式解决了问题(如何使用 spring 和 Hibernate 连接多个数据库),我希望它会有所帮助:)
注意: 我已经添加了相关代码,请借助我在下面提到的代码中使用的 impl 来制作 dao。
I solved the problem (How to connect multiple database using spring and Hibernate) in this way, I hope it will help :)
NOTE: I have added the relevant code, kindly make the dao with the help of impl I used in the below mentioned code.
查看此 Wiki 页面 http://code.google.com/p /gwt-spring-jpa-lucene/wiki/PersistenceUnitSetup 详细介绍了如何使用 persistenceXmlLocations 和 DAO 中的注释来配置 spring
Check this wiki page http://code.google.com/p/gwt-spring-jpa-lucene/wiki/PersistenceUnitSetup that details how to configure spring with persistenceXmlLocations and the annotations in DAO
在那些将被注入的entityManagerFactories之间切换
switch among those entityManagerFactories those will be injected