正确的Hibernate映射

时间:2012-07-02 04:23:01

标签: hibernate hbm

我有五张桌子及其各自的栏目

大学

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

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

1 个答案:

答案 0 :(得分:1)

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

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

除了ids本身是唯一的,然后将它们作为id映射更容易。引用像这样映射。

<many-to-one name="country" column="id_country" />