【问题标题】:Hibernate Mapping two tables in one hbm file with same class name?Hibernate在一个具有相同类名的hbm文件中映射两个表?
【发布时间】:2012-08-21 22:34:25
【问题描述】:

我有 2 张桌子:

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
  )

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

我有一个表格,上面的所有列都是字段。但是,表FTP_SCAN_EVENTS 中的列是隐藏的,通过选择(REVRECOVERY) 复选框,它们会显示在表单中。并且用户可以根据需要动态添加行。

<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>

我的位置 hbm 文件:

<?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>

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

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

【问题讨论】:

  • 您必须更具体。我假设您的意思是 或 @SecondaryTable。但您也可能意味着将您的 Location 类映射到单独的第二个表。请澄清。
  • 位置类到单独的第二个表。两个表的关系是 PK 和 FK。

标签: hibernate modelattribute


【解决方案1】:

根据您的说明,您想查看&lt;join/&gt;。但是,由于您没有具体说明,因此我只能笼统地为您提供帮助。基本上&lt;join/&gt;(与 JPA @SecondaryTable 的功能相同)允许您将 2 个表之间的连接列视为一个组合行来定义实体的基本类型。有一些注意事项,例如期望“共享主键”(也称为真正的一对一)集;有关完整详细信息,请参阅文档。无论如何,假设您有一个名为 location_supp 的辅助表和一个名为 location_id 的 PK 引用(FK)到 Location.LocationId,您会说:

<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>

【讨论】:

  • 这对我有用..但是,保存数据时会有什么影响?
  • 我正在加入表格......所以我需要将我的 setter 和 getter 方法放在同一个类(位置)中,对吧?
  • 我需要创建一个不同的 DAO 类来读取第二个表吗?
  • 哎呀!加入正在影响整个应用程序。我在位置表中有一堆位置..现在,它只显示 location_supp 表中的一个..!
  • 呃。这就是我要求你澄清的原因。所以听起来你真的很喜欢&lt;many-to-one/&gt;。但在你澄清你的问题之前,我已经完成了帮助。这是模糊的。你希望你的课程是什么样的?你的桌子是什么样子的?
【解决方案2】:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.url.hiber.action">
    <class name="AxisFandAuser" table="axisFandA_user" dynamic-insert="true" dynamic-update="true">

     <id name="userId" column="userId">
        <generator class="native" />
    </id>   
     <property name="userFName" column="userFName"/>

    <property name="userMName" column="userMName"/>

     <property name="userLName" column="userLName"/>

    <property name="username" column="username"/>

     <property name="userEmailId" column="userEmailId"/>

    <property name="userOfficeLNumber" column="userOfficeLNumber"/>

    <property name="userExtention" column="userExtention"/>

     <property name="userMNumber" column="userMNumber"/>

    <property name="userPassword" column="userPassword"/>

    <property name="lock" column="lock"/>

    <!-- <property name="roleId" column="roleId"/>-->

    <many-to-one name="axisFandARole" class="com.url.hiber.action.AxisFandARole" fetch="select">
    <column name="roleId" not-null="false" />
</many-to-one>
    <set name="axisFandAservice" table="axisFandA_service" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="userId" not-null="false" />
        </key>
        <one-to-many class="com.url.hiber.action.AxisFandAservice" />
</set>

<set name="axisFandAgroundRule" table="axisFandA_groundRule" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="userId" not-null="false" />
        </key>
        <one-to-many class="com.url.hiber.action.AxisFandAgroundRule" />
</set>
<set name="quantumLogDetail" table="axisFandA_quantumLogDetail" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="userId" not-null="false" />
        </key>
        <one-to-many class="com.url.hiber.action.QuantumLogDetail" />
    </set>
  </class>  
</hibernate-mapping>

【讨论】:

    猜你喜欢
    • 2018-05-05
    • 2017-01-09
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    相关资源
    最近更新 更多