2011-06-20 41 views
0

我必須在我的項目(Web應用程序),使用Hibernate/JPA映射三個數據庫,所以我的問題是如何配置persistance.xml支持三種連接 這是我的文件: persistance.xml地圖三個數據庫的同時

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" 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_2_0.xsd"> 
<persistence-unit name="JiraMapPU" transaction-type="RESOURCE_LOCAL"> 
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
<class>DTOKpi.AssociationKpiProfil</class> //from database1 
<class>DTOKpi.Kpi</class>     //database1 
<class>DTOKpi.ProfilUser</class>   // database1 
<class>DTOJIRA.Resolution</class>   // database2 
<class>DTOJIRA.Project</class>   // database2 
<class>DTOJIRA.Issuestatus</class> 
<class>DTOJIRA.Jiraissue</class> 
<class>DTOJIRA.Priority</class> 
<class>DTOJIRA.Component</class> 
<class>DTOJIRA.Issuetype</class> 
<exclude-unlisted-classes>false</exclude-unlisted-classes> 
<properties> 
    <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jiradb"/> 
    <property name="javax.persistence.jdbc.password" value="root"/> 
    <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> 
    <property name="javax.persistence.jdbc.user" value="root"/> 
</properties> 

我的hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
<session-factory> 
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jiradb</property> 
<property name="hibernate.connection.username">root</property> 
<mapping resource="DTOTest/issuestatus.hbm.xml"/> 
<mapping resource="hibernate.hbm.xml"/> 
</session-factory> 
</hibernate-configuration> 

我發現這個鏈接有用:How do I connect to multiple databases using JPA?,但我不明白的第二個步驟

回答

1

如果你的三個數據庫是我想它會工作是相同的,但如果沒有,我想你應該創建一個映射文件每個數據庫。

+0

你好我找到了解決方案,我已經添加另一<持久性單元>爲我的第二數據庫:) – ramram