我必须使用Hibernate / JPA在我的项目(Web应用程序)上映射三个数据库,所以我的问题是如何配置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?但我无法理解第二步
答案 0 :(得分:1)
如果你的三个数据库是相同的,我想它会起作用,但如果没有,我认为你应该为每个数据库创建一个地图文件。