0

我有五个表和它们各自的列

大学

 - id_university (pk)
  - id_country (pk)   ---- FK ---- country.id_country
  - univ_code
  - univ_name
  - id_update  FK ---- update.id_update

研究所

  • id_institute (pk)
  • id_country (pk) ---- FK ---- country.id_country
  • id_univ
  • id_address FK ---- update.id_update

地址

- id_address
    - id_country (pk) ---- FK ---- country.id_country
    - addressfield

国家

- id_country

更新

- id_update 
        id_country (pk) ---- FK ---- country.id_country

这些是 hbm 映射

<class name="xxxxxx" table="university">
        <id name="id_university " column="id_university ">
            <generator class="increment" />
        </id>

        <property name="id_country ">
            <column name="id_country " />
        </property>
        <property name="univ_code">
            <column name="univ_code" />
        </property>
        <property name="univ_name">
            <column name="univ_name" />
        </property>
        <property name="id_update">
            <column name="id_update" />
        </property>
</class>



<class name="xxxxxx" table="institute">
        <id name="id_institute " column="id_institute ">
            <generator class="increment" />
        </id>

        <property name="id_country ">
            <column name="id_country " />
        </property>
        <property name="id_univ">
            <column name="id_univ" />
        </property>
        <property name="id_address">
            <column name="id_address" />
        </property>
        <property name="id_update">
            <column name="id_update" />
        </property>
</class>

<class name="xxxxxx" table="address">
        <id name="id_address " column="id_address ">
            <generator class="increment" />
        </id>

        <property name="id_country ">
            <column name="id_country " />
        </property>
        <property name="addressfield">
            <column name="addressfield" />
        </property>
</class>


<class name="xxxxxx" table="address">
        <id name="id_address " column="id_address ">
            <generator class="increment" />
        </id>

        <property name="id_country ">
            <column name="id_country " />
        </property>
        <property name="addressfield">
            <column name="addressfield" />
        </property>
</class>

由于存在外键关系,有人可以指导我使用休眠映射。我无法获得正确的映射。

4

1 回答 1

1

由于表的主键不止一列,因此您需要将它们映射为 compositeId

<composite-id>
  <key-property name="id" column="id_university" />
  <key-reference name="country" column="id_country" />
</id>

除了 id 本身是唯一的,那么将它们映射为 id 会更容易。引用是这样映射的。

<many-to-one name="country" column="id_country" />
于 2012-07-02T05:51:51.953 回答