Hibernate Mapping two tables in one hbm file with same class name?

≡放荡痞女 提交于 2019-12-10 23:52:58

问题


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.


回答1:


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>



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


来源:https://stackoverflow.com/questions/12141972/hibernate-mapping-two-tables-in-one-hbm-file-with-same-class-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!