Hibernate + Nicht primary key als foreign key

Romsl

Erfahrenes Mitglied
Hi,

besteht in hibernate die Möglichkeit in einer Tabelle eine Spalte (nicht primary key) der Tabelle A als foreign key der Tabelle B zu verwenden?

Beispiel:

Code:
 <class name="ebooking.module.base.bean.system.SystemLocale" table="BASE_SYSTEM_LOCALE">
        <id name="id" type="long">
            <column name="ID" not-null="true"/>
            <generator class="increment"/>
        </id>

        <property name="localeKey" type="string">
            <column name="LOCALE_KEY" not-null="true" unique="true"/>
        </property>

        <property name="language" type="string">
            <column name="LANGUAGE" not-null="true"/>
        </property>

        <property name="countryName" type="string">
            <column name="COUNTRY_NAME" not-null="true"/>
        </property>

        <set name="countries" table="BASE_ADDRESS_COUNTRY" inverse="true" cascade="all-delete-orphan" lazy="true" sort="ebooking.module.base.hbm.HibernateComparator">
            <key column="SYSTEM_LOCALE_KEY" property-ref="localeKey" unique="true"/>
            <one-to-many class="ebooking.module.base.bean.address.Country"/>
        </set>
    </class>

Code:
<class name="ebooking.module.base.bean.address.Country" table="BASE_ADDRESS_COUNTRY">
        <id name="id" type="long">
            <column name="ID" not-null="true"/>
            <generator class="increment"/>
        </id>

        <property name="name" type="string">
            <column name="NAME" length="100" not-null="true"/>
        </property>

        <many-to-one name="systemLocale" column="SYSTEM_LOCALE_KEY" class="ebooking.module.base.bean.system.SystemLocale" cascade="all-delete-orphan" not-null="true" property-ref="localeKey"/>

        <set name="counties" table="BASE_ADDRESS_COUNTY" inverse="true" cascade="save-update" lazy="true" sort="ebooking.module.base.hbm.HibernateComparator">
            <key column="COUNTRY_ID"/>
            <one-to-many class="ebooking.module.base.bean.address.County"/>
        </set>
        <!--
        <set name="counties" table="BASE_ADDRESS_COUNTY" cascade="all">
            <key column="ADDRESS_COUNTRY_ID"/>
            <one-to-many class="ebooking.module.base.bean.address.County"/>
        </set>
        -->
    </class>

So hab ich das, funktioniert aber nicht. Es wird kein foreign key constraint angelegt
 
Es funktioniert doch. Aber warum wird kein fk constraint angelegt? Und warum wird die Spalte LOCALE_KEY nicht auf unique gesetzt?
 
Foreign Key Constrainsts müssen immer auf Primary Keys der referenzierten Tabelle verweisen. So sind Foreign Keys definiert. Sonst macht es weder in relationalem Modell noch im objektorientiertem Model Sinn.

mfg.
JR
 
Zurück