Hibernate在一个hbm文件中使用相同的类名映射两个表? [英] Hibernate Mapping two tables in one hbm file with same class name?

查看:82
本文介绍了Hibernate在一个hbm文件中使用相同的类名映射两个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表:

  CREATE TABLELOCATION(
IDNUMBER(19,0 )NOT NULL ENABLE,
VERSIONNUMBER(19,0)NOT NULL ENABLE,
DELETEULDNUMBER(1,0)DEFAULT 0 NOT NULL ENABLE,
INBOUNDNUMBER 1,0)DEFAULT 0 NOT NULL ENABLE,
AAENABLEDNUMBER(1,0)DEFAULT 0 NOT NULL ENABLE,
WSUPLDTOOLNUMBER(1,0)DEFAULT 0 NOT NULL ENABLE,
CISDESTVARCHAR2(7 CHAR),
REVRECOVERYNUMBER(1,0)DEFAULT 0,
CONSTRAINTLOCATION_IDPRIMARY KEY(ID)ENABLE)

$ CREATE TABLETSLD164。FTP_SCAN_EVENTS

HOSTNAMEVARCHAR2(200 BYTE),
DIRECTORYVARCHAR2(200 BYTE),
USERNAMEVARCHAR2 (20 BYTE),
PASSWORDVARCHAR2(20 BYTE),
LOCATION_IDNUMBER(19,0)NOT NULL ENABLE,
CONSTRAINTFTP_SCAN_EVENTS_LOCATION_FK1FOREIGN KEY(LOCATION_ID)参考文献L. OCA(ID)ENABLE

与FK和PK相关的表格(Location_Id列和Id)



我有一个表单,所有上面的列作为字段。但是,表 FTP_SCAN_EVENTS 中的列是隐藏的,并且通过选择复选框(REVRECOVERY),它们将显示在表单中。如果他愿意,用户可以动态添加行。

 < td>< form:checkbox path =revRecoveryonclick =showMe('div1',this), SHOWME( 'I1',这一点)/> 
< input type =hiddenvalue =1name =_ revRecovery/>
FTP扫描事件< / td>< / tr>< tr>< td colspan =4>
< table id =div1style =display:none>
< tr>< td>< input type =textvalue =hostnameonfocus =if(this.value =='hostname'){this.value =''}onblur = if(this.value ==''){this.value ='hostname'}size =30maxlength =200/>< / td>
< td>< input type =textvalue =directoryonfocus =if(this.value =='directory'){this.value =''}onblur =if .value ==''){this.value ='directory'}size =30maxlength =200/>< / td>
< td>< input type =textvalue =usernameonfocus =if(this.value =='username'){this.value =''}onblur =if .value ==''){this.value ='username'}size =20maxlength =20/>< / td>
< td>< input type =textvalue =passwordonfocus =if(this.value =='password'){this.value =''}onblur =if .value ==''){this.value ='password'}size =20maxlength =20/>< / td>< / tr>
< / table>
< / td>< / tr>
< tr id =i1style =display:none>< td>< input type =buttononclick =addRow()value =+/>
< input type =buttononclick =removeRowFromTable(); value = - />
< / td>< / tr>

我的hbm文件位置:

 <?xml version =1.0?> 
<!DOCTYPE hibernate-mapping PUBLIC - // Hibernate / Hibernate映射DTD // ENhttp://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">
< hibernate-mapping package =ca.ups.tundra.model>
< class name =Locationtable =LOCATION>
< id name =idaccess =fieldtype =long>
< generator class =native/>
< / id>
< version name =versionaccess =fieldcolumn =VERSIONtype =long/>

< property name =cisDesttype =stringcolumn =CISDESTlength =7not-null =true/>
< property name =revRecoverytype =booleannot-null =true/>
< property name =deleteUldtype =booleannot-null =true/>
< property name =inboundtype =booleannot-null =true/>
< property name =aaEnabledtype =booleannot-null =true/>
< property name =wsUpldTooltype =booleannot-null =true/>
< property name =locationTypetype =stringcolumn =LOCATIONTYPElength =2not-null =true/>
< key column =LOCATION_ID/>
< / set>
< / class>
< / hibernate-mapping>

Location的模型类只是setter和getter方法。

我需要将我的第二个表映射到与上面相同的映射文件中,并且需要为getter和setter方法使用相同的模型类。

解决方案

根据您的说明,您希望查看< join /> 。但是,由于您没有提供具体细节,因此我只能在一般条件下为您提供帮助。基本上< join /> (与JPA @SecondaryTable 相同的功能)允许您处理2表作为一个组合行来定义实体的基本类型。有一些警告,比如期待共享主键(又名真正的一对一)集;详细信息请参阅文档。无论如何,假设您有一个名为 location_supp 的辅助表格,其中名为 location_id 的辅助表格将FK指向 Location.LocationId ,你会说:

 < class name =Location table =位置...> 
< id name =idcolumn =LocationId... />
< property name =namecolumn =name... />
<! - 映射表中其他列的映射 - >

< join table =location_supp>
<! -
key定义假设为
的连接表的pk列,也是返回位置
- >的外键源列。
< key column =location_id/>

<! - 映射来自location_supp表的列的映射 - >
< / join>

< / class>


I have 2 tables:

CREATE TABLE "LOCATION"   (
    "ID"              NUMBER(19,0) NOT NULL ENABLE,
    "VERSION"         NUMBER(19,0) NOT NULL ENABLE,
    "DELETEULD"         NUMBER(1,0) DEFAULT 0 NOT NULL ENABLE,
    "INBOUND"           NUMBER(1,0) DEFAULT 0 NOT NULL ENABLE,
    "AAENABLED"         NUMBER(1,0) DEFAULT 0 NOT NULL ENABLE,
    "WSUPLDTOOL"        NUMBER(1,0) DEFAULT 0 NOT NULL ENABLE,
    "CISDEST"           VARCHAR2(7 CHAR),
    "REVRECOVERY"       NUMBER(1,0) DEFAULT 0,
    CONSTRAINT "LOCATION_ID" PRIMARY KEY ("ID")  ENABLE  )

CREATE TABLE "TSLD164"."FTP_SCAN_EVENTS"
  (
    "HOSTNAME"    VARCHAR2(200 BYTE),
    "DIRECTORY"   VARCHAR2(200 BYTE),
    "USERNAME"    VARCHAR2(20 BYTE),
    "PASSWORD"    VARCHAR2(20 BYTE),
    "LOCATION_ID" NUMBER(19,0) NOT NULL ENABLE,
    CONSTRAINT "FTP_SCAN_EVENTS_LOCATION_FK1" FOREIGN KEY ("LOCATION_ID") REFERENCES "LOCATION" ("ID") ENABLE
  )

Tables related with FK and PK (Location_Id column and Id)

I have a form with all the above columns as fields. But, the columns from table FTP_SCAN_EVENTS are hidden and by selection of a checkbox (REVRECOVERY) they show up in the form. And user can add rows dynamically If he wants.

<td><form:checkbox path="revRecovery" onclick="showMe('div1',this), showMe('i1',this)"/>
            <input type="hidden" value="1" name="_revRecovery"/>
            FTP Scan Events</td></tr><tr><td colspan="4">
                    <table id="div1" style="display:none">
            <tr><td><input type="text" value="hostname" onfocus="if(this.value == 'hostname'){this.value =''}" onblur="if(this.value == ''){this.value ='hostname'}" size="30" maxlength="200"/></td>
            <td><input type="text" value="directory" onfocus="if(this.value == 'directory'){this.value =''}" onblur="if(this.value == ''){this.value ='directory'}" size="30" maxlength="200"/></td>
            <td><input type="text" value="username" onfocus="if(this.value == 'username'){this.value =''}" onblur="if(this.value == ''){this.value ='username'}" size="20" maxlength="20"/></td>
            <td><input type="text" value="password" onfocus="if(this.value == 'password'){this.value =''}" onblur="if(this.value == ''){this.value ='password'}" size="20" maxlength="20"/></td></tr>
                    </table>
            </td></tr>
            <tr id="i1" style="display:none"><td><input type="button" onclick="addRow()" value="+"/>
            <input type="button" onclick="removeRowFromTable();" value="-" />
            </td></tr>

my hbm file for Location:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="ca.ups.tundra.model">
    <class name="Location" table="LOCATION">
        <id name="id" access="field" type="long">
            <generator class="native"/>
        </id>
        <version name="version" access="field" column="VERSION" type="long"/>

        <property name="cisDest" type="string" column="CISDEST" length="7" not-null="true"/>
        <property name="revRecovery" type="boolean" not-null="true"/>
        <property name="deleteUld" type="boolean" not-null="true"/>
        <property name="inbound" type="boolean" not-null="true"/>
        <property name="aaEnabled" type="boolean" not-null="true"/>
        <property name="wsUpldTool" type="boolean" not-null="true"/>        
        <property name="locationType" type="string" column="LOCATIONTYPE" length="2" not-null="true"/>
        <set name="groups" table="LOCATIONGROUPS" cascade="save-update" access="field">
            <key column="LOCATION_ID"/>
            <many-to-many class="LocationGroup" column="GROUP_ID"/>
        </set>
    </class>
</hibernate-mapping>

Model class for Location is just setter and getter methods.

I need to map my second table in the same mapping file as above and need to use the same model class for getter and setter methods.

解决方案

Based on your clarification, you want to look at <join/>. However, as you gave no specifics I can only help you in general terms. Basically <join/> (same functionality as JPA @SecondaryTable) allows you to treat the joined columns between the 2 tables as one combined row to define the basic types for your entity. There are some caveats to that such as expecting "shared primary key" (aka a true one-to-one) set; see the documentation for full details. Anyway, assuming you have a secondary table named location_supp with a PK named location_id that refers back (FK) to Location.LocationId, you would say:

<class name="Location" table="Location" ...>
    <id name="id" column="LocationId" .../>
    <property name="name" column="name" .../>
    <!-- mappings for other columns from Location table -->

    <join table="location_supp">
        <!-- 
            key defines the pk column of that joined table which is assumed to 
            also be the foreign-key source column referring back to Location
        -->
        <key column="location_id"/>

        <!-- mappings for columns from location_supp table -->
    </join>

</class>

这篇关于Hibernate在一个hbm文件中使用相同的类名映射两个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆