2013-01-19 86 views
0

我目前有以下持久性xml文件,我用它來創建兩個數據庫,其中一個包含用戶表(從一個Java實體類映射),另一個映射表的怪物(作爲以上)。持久性XML 2表

我完全陌生的JPA,這是我第一次使用它,到目前爲止,我還設法湊齊以下XML代碼:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="1.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_1_0.xsd"> 
    <persistence-unit name="monsters" transaction-type="RESOURCE_LOCAL"> 
    <class>databaseManagement.Monster</class> 
    <properties> 
     <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> 
     <property name="hibernate.connection.username" value=""/> 
     <property name="hibernate.connection.password" value=""/> 
     <property name="hibernate.connection.url" value="jdbc:hsqldb:monsters"/> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> 
     <property name="hibernate.hbm2ddl.auto" value="update"/> 
    </properties> 
    </persistence-unit> 
    <persistence-unit name="users" transaction-type="RESOURCE_LOCAL"> 
    <class>databaseManagement.User</class> 
    <properties> 
     <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> 
     <property name="hibernate.connection.username" value=""/> 
     <property name="hibernate.connection.password" value=""/> 
     <property name="hibernate.connection.url" value="jdbc:hsqldb:users"/> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> 
     <property name="hibernate.hbm2ddl.auto" value="update"/> 
    </properties> 
    </persistence-unit> 
</persistence> 

不過,我要的是用戶和怪物表是一個數據庫中的兩個表格,而不是兩個單獨的數據庫中的兩個表格,但是我不知道如何執行此操作。

非常感謝,我真的很卡住爲如何實現這一點,任何幫助表示讚賞:)

回答

1

這是非常簡單的,你必須刪除持久單元並添加刪除類剩下的單位。

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="1.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_1_0.xsd"> 
    <persistence-unit name="monsters" transaction-type="RESOURCE_LOCAL"> 
    <class>databaseManagement.Monster</class> 
    <class>databaseManagement.User</class> 
    <properties> 
     <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> 
     <property name="hibernate.connection.username" value=""/> 
     <property name="hibernate.connection.password" value=""/> 
     <property name="hibernate.connection.url" value="jdbc:hsqldb:monsters"/> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> 
     <property name="hibernate.hbm2ddl.auto" value="update"/> 
    </properties> 
    </persistence-unit> 
</persistence> 

在這裏尋找更多example