Domanda

I have a five tables and their respective column

university

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

institute

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

address

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

country

- id_country

update

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

These are the hbm mappings

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

As there are foreign key relationships, can somebody guide me with hibernate mapping. I am unable to get the correct mapping.

È stato utile?

Soluzione

since the primar key of the tables are more than one column you will need to map them as compositeId

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

except the ids are unique on their own then it would be easier all around to map them as id. The references are mapped like this.

<many-to-one name="country" column="id_country" />
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top