春季jpa冬眠与更多的数据源 [英] spring jpa hibernate with more datasources
问题描述
我必须在Hibernate,Jpa的应用程序(spring)中使用两个不同的数据库。
我想将不同的表格直接定义到不同的数据源。
所以我使用了两个不同的持久化单元,我尝试使用
< property name =packagesToScanvalue = it.two.app.domain.first/>
和
< property name =packagesToScanvalue =it.two.app.domain.second/>
将不同的表放入不同的包中。
但它不起作用。
事实上,所有的表都与第一个数据源相关。
,然后我试图写入持久化XML文件中类
的名称,例如
< / p> persistence-unit name =persistenceFirsttransaction-type =RESOURCE_LOCAL>
< class> it.two.app.domain.first.OneTable< / class>
< exclude-unlisted-classes />
< / persistence-unit>
和
it.two.app.domain.second.OtherTable
但是,当我运行日志说
表'firstDB.other-table'不存在
和我使用到服务文件中
$ pre $ c $ @PersistenceContext(unitName =persistenceFirst)
private EntityManager em;
和
@PersistenceContext(unitName =persistenceSecond)
EntityManager em;
你有一些想法吗?
Thi是数据源XML文件
<?xml version =1.0encoding =UTF-8 >?;
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xmlns:context =http://www.springframework.org/schema/context
xmlns:jdbc =http://www.springframework.org/schema/jdbc
xmlns :jpa =http://www.springframework.org/schema/data/jpa
xmlns:tx =http://www.springframework.org/schema/tx
xmlns:p =http://www.springframework.org/schema/p
xsi:schemaLocation =http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/ jdbc / spring-jdbc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework .org / schema / tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http:// www。 springframework.org/模式/上下文/弹簧上下文3.1.xsd>
<! - 第一个数据源 - >
< context:property-placeholder location =classpath:jdbc-first.properties/>
p:driverClassName =$ {jdbc.driverClassName}p:url =$ {jdbc.databaseurl }
p:username =$ {jdbc.username}p:password =$ {jdbc.password}/>
<! - 秒数据源 - >
< context:property-placeholder location =classpath:jdbc-second.properties/>
< bean id =dataSourceSecondclass =org.apache.commons.dbcp.BasicDataSource
p:driverClassName =$ {jdbc.driverClassName}p:url =$ {jdbc.databaseurl }
p:username =$ {jdbc.username}p:password =$ {jdbc.password}/>
< bean id =transactionManagerFirstclass =org.springframework.orm.jpa.JpaTransactionManager
p:entityManagerFactory-ref =emfFirst/>
< bean id =transactionManagerSecondclass =org.springframework.orm.jpa.JpaTransactionManager
p:entityManagerFactory-ref =emfSecond/>
< tx:annotation-driven transaction-manager =transactionManagerFirst/>
< tx:annotation-driven transaction-manager =transactionManagerSecond/>
entity-manager-factory-ref =emfFirsttransaction-manager -ref =transactionManagerFirst/>
< jpa:repositories base-package =it.two.app.repository.second
entity-manager-factory-ref =emfSecondtransaction-manager-ref = transactionManagerSecond/>
< bean id =emfFirst
class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =persistenceUnitNamevalue =persistenceFirst/>
< property name =persistenceXmlLocationvalue =classpath:/META-INF/persistence-first.xml/>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter/>
< / property>
< property name =dataSourceref =dataSourceFirst/>
< property name =packagesToScanvalue =it.two.app.domain.first/>
< property name =jpaProperties>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.MySQLDialect< / prop>
< prop key =hibernate.max_fetch_depth> 3< / prop>
< prop key =hibernate.jdbc.fetch_size> 50< / prop>
< prop key =hibernate.jdbc.batch_size> 10< / prop>
< /道具>
< / property>
< / bean>
< bean id =emfSecondclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =persistenceUnitNamevalue =persistenceSecond/>
< property name =persistenceXmlLocationvalue =classpath:/ META-INF / persistence- second.xml/>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter/>
< / property>
< property name =dataSourceref =dataSourceSecond/>
< property name =packagesToScanvalue =it.two.app.domain.second/>
< property name =jpaProperties>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.MySQLDialect< / prop>
< prop key =hibernate.max_fetch_depth> 3< / prop>
< prop key =hibernate.jdbc.fetch_size> 50< / prop>
< prop key =hibernate.jdbc.batch_size> 10< / prop>
< /道具>
< / property>
< / bean>
< / beans>
解决方案!!!!!!
我解决了这个问题。
简单
<! - first datasource - >
p:driverClassName =com.mysql.jdbc.Driverp:url =url ... 。
p:username =usernamep:password =password/>
<! - 秒数据源 - >
< bean id =dataSourceSecondclass =org.apache.commons.dbcp.BasicDataSource
p:driverClassName =com.mysql.jdbc.Driverp:url =url2 ... 。
p:username =username2p:password =password2/>
如果您想使用多个 DataSource
位于 Spring + JPA
。
- 在
persistence.xml
中创建两个或多个PersistenceUnit
。
- 创建<$对于
spring-beans.xml
中的每个PersistenceUnit
,c $ c> EntityManagerFactory li>
- 创建<$对于
更多参考资料。
- 多个数据库使用Spring + Hibernate + JPA
- 使用Spring 3,Hibernate 3访问多个数据库 12 / handling-transaction-with-multiple.htmlrel =nofollow noreferrer>使用Spring 3.0和Hibernate 3.0的多个数据库
$ b $在您的DAO类中。
@PersistenceContext(unitName =JPA_1)
private EntityManager em_1;
@PersistenceContext(unitName =JPA_2)
私人EntityManager em_2;
Conig persistence.xml
< persistence-unit name =JPA_1type =RESOURCE_LOCAL>
....
< / persistence-unit>
< persistence-unit name =JPA_2type =RESOURCE_LOCAL>
....
< / persistence-unit>
配置:spring-beans.xml
< bean id =entityManagerFactory1class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =persistenceUnitNamevalue =JPA_1/>
< property name =jpaVendorAdapterref =jpaVendorAdapter/>
< property name =jpaDialect>
< bean class =org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect/> < - 如果有必要,请用休眠替换。
< / property>
< property name =jpaPropertyMap>
<道具>
< prop key =eclipselink.weaving> false< / prop> < - 如果有必要,请用休眠替换。
< /道具>
< / property>
< property name =loadTimeWeaver>
< bean class =org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver>
< / bean>
< / property>
< / bean>
< bean id =entityManagerFactory2class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =persistenceUnitNamevalue =JPA_2/>
< property name =jpaVendorAdapterref =jpaVendorAdapter/>
< property name =jpaDialect>
< bean class =org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect/> < - 如果有必要,请用休眠替换。
< / property>
< property name =jpaPropertyMap>
<道具>
< prop key =eclipselink.weaving> false< / prop> < - 如果有必要,请用休眠替换。
< /道具>
< / property>
< property name =loadTimeWeaver>
< bean class =org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver>
< / bean>
< / property>
< / bean>
< bean id =jpaVendorAdapterclass =org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter> < - 如果有必要,请用休眠替换。
< property name =databasePlatformvalue =org.eclipse.persistence.platform.database.MySQLPlatform/>
<! - < property name =databasePlatformvalue =org.eclipse.persistence.platform.database.OraclePlatform/> - >
< property name =generateDdlvalue =false/>
< property name =showSqlvalue =true/>
< / bean>
I have to use two different database in my application(spring) with Hibernate,Jpa. I'd like to define the different table directly to the different data sources. So I use two different persistence unit and I try to use
<property name="packagesToScan" value="it.two.app.domain.first" />
and
<property name="packagesToScan" value="it.two.app.domain.second" />
putting the different tables into the different packages. but It doesn't work. Infact all the table is with the first data source. then I tried to write into the perstistence XML file the name of the class like
<persistence-unit name="persistenceFirst" transaction-type="RESOURCE_LOCAL">
<class>it.two.app.domain.first.OneTable</class>
<exclude-unlisted-classes/>
</persistence-unit>
and it.two.app.domain.second.OtherTable
But when I run Log says Table 'firstDB.other-table' doesn't exist and I use into the services file
@PersistenceContext(unitName ="persistenceFirst")
private EntityManager em;
and
@PersistenceContext(unitName = "persistenceSecond")
EntityManager em;
Have you got some Ideas? Thi is the data sources XML file
<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- first datasource -->
<context:property-placeholder location="classpath:jdbc-first.properties"/>
<bean id="dataSourceFirst" class="org.apache.commons.dbcp.BasicDataSource"
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}"
p:username="${jdbc.username}" p:password="${jdbc.password}" />
<!-- second datasource -->
<context:property-placeholder location="classpath:jdbc-second.properties"/>
<bean id="dataSourceSecond" class="org.apache.commons.dbcp.BasicDataSource"
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}"
p:username="${jdbc.username}" p:password="${jdbc.password}" />
<bean id="transactionManagerFirst" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="emfFirst"/>
<bean id="transactionManagerSecond" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="emfSecond"/>
<tx:annotation-driven transaction-manager="transactionManagerFirst"/>
<tx:annotation-driven transaction-manager="transactionManagerSecond"/>
<jpa:repositories base-package="it.two.app.repository.first"
entity-manager-factory-ref="emfFirst" transaction-manager-ref="transactionManagerFirst" />
<jpa:repositories base-package="it.two.app.repository.second"
entity-manager-factory-ref="emfSecond" transaction-manager-ref="transactionManagerSecond" />
<bean id="emfFirst"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistenceFirst"/>
<property name="persistenceXmlLocation" value="classpath:/META-INF/persistence-first.xml"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="dataSource" ref="dataSourceFirst" />
<property name="packagesToScan" value="it.two.app.domain.first" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
</props>
</property>
</bean>
<bean id="emfSecond" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistenceSecond"/>
<property name="persistenceXmlLocation" value="classpath:/META-INF/persistence- second.xml"/>
<property name="jpaVendorAdapter" >
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="dataSource" ref="dataSourceSecond"/>
<property name="packagesToScan" value="it.two.app.domain.second"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
</props>
</property>
</bean>
</beans>
SOLUTION!!!!!! I undestand the problem. Simply
<!-- first datasource -->
<bean id="dataSourceFirst" class="org.apache.commons.dbcp.BasicDataSource"
p:driverClassName="com.mysql.jdbc.Driver" p:url="url...."
p:username="username" p:password="password" />
<!-- second datasource -->
<bean id="dataSourceSecond" class="org.apache.commons.dbcp.BasicDataSource"
p:driverClassName="com.mysql.jdbc.Driver" p:url="url2...."
p:username="username2" p:password="password2" />
If you would like to use multiple DataSource
in Spring + JPA
.
- Create two or more
PersistenceUnit
inpersistence.xml
. - Create
EntityManagerFactory
for eachPersistenceUnit
inspring-beans.xml
.
More Reference.
- Multiple database with Spring+Hibernate+JPA
- Access Multiple Database Using Spring 3, Hibernate 3
- Multiple Database using Spring 3.0 and Hibernate 3.0
In your DAO classes.
@PersistenceContext(unitName ="JPA_1")
private EntityManager em_1;
@PersistenceContext(unitName ="JPA_2")
private EntityManager em_2;
Conig persistence.xml
<persistence-unit name="JPA_1" type="RESOURCE_LOCAL">
....
</persistence-unit>
<persistence-unit name="JPA_2" type="RESOURCE_LOCAL">
....
</persistence-unit>
Config : spring-beans.xml
<bean id="entityManagerFactory1" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="JPA_1"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect"/> <--if it is necessary, replace with hibernate.
</property>
<property name="jpaPropertyMap">
<props>
<prop key="eclipselink.weaving">false</prop> <--if it is necessary, replace with hibernate.
</props>
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver">
</bean>
</property>
</bean>
<bean id="entityManagerFactory2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="JPA_2"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect"/> <--if it is necessary, replace with hibernate.
</property>
<property name="jpaPropertyMap">
<props>
<prop key="eclipselink.weaving">false</prop> <--if it is necessary, replace with hibernate.
</props>
</property>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver">
</bean>
</property>
</bean>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"> <--if it is necessary, replace with hibernate.
<property name="databasePlatform" value="org.eclipse.persistence.platform.database.MySQLPlatform"/>
<!--<property name="databasePlatform" value="org.eclipse.persistence.platform.database.OraclePlatform" />-->
<property name="generateDdl" value="false"/>
<property name="showSql" value="true"/>
</bean>
这篇关于春季jpa冬眠与更多的数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!