Hi,
I have to map a legacy database and tried to define a bi-directional <one-to-many> relationship between two classes, where the foreign-key relationship is not established with the primary key, but with an alternate column/property with an unique constraint. Here the mapping:
Code:
<class name="BasisPickListe" table="PICKLISTEN">
<composite-id>
<key-property name="nummer" column="NR_PICKL" type="string"/>
<key-many-to-one name="lager" lazy="false">
<column name="LAGER"/>
</key-many-to-one>
</composite-id>
<discriminator type="string" formula="...">
<property name="sendungsnummer" column="SEND_NR" type="string"/>
</class>
<subclass name="PickListe" extends="BasisPickListe" discriminator-value="PickListe">
<property name="empfaengerId" column="ID_EMPFAENGER" type="string"/>
<many-to-one name="sammelPickListe"
class="SammelPickListe"
column="ID_EMPFAENGER"
property-ref="sendungsnummer"
not-null="false"
not-found="ignore"/>
</subclass>
<subclass name="SammelPickListe" extends="BasisPickListe" discriminator-value="SammelPickListe">
<map name="mSammelPickListen" inverse="true">
<key property-ref="empfaengerId" column="SEND_NR" />
<map-key formula="NR_PICKL" type="string"/>
<one-to-many class="PickListe"/>
</map>
</subclass>
The problem ist the <map> definition in the subclass "SammelPickListe". I get the error "org.hibernate.MappingException: property [empfaengerId] not found on entity [com.rhenus.rml.database.lvs.helas.tables.SammelPickListe]" with this mapping definition.
I tried to define the mapping according to the sample "Associations on alternate keys" (
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/example-mappings.html#example-mappings-association-alternatekeys).
Hibernate seems to look for the property "empfaengerId" in the subclass "SammelPickListe". But "empfaengerId" is the foreign key property in the class "PickListe". The join should be made by the clause SammelPickListe.SEND_NR = PickListe.empfaengerId.
Currently I select the content of the map with the following Java code (as workaround):
Code:
Criterion restriction = Restrictions.eq("empfaengerId", sammelPickListe.getSendungsnummer());
Criteria selectPickLists = session.createCriteria(BasisPickListe.class).add(restriction);
List<BasisPickListe> pickLists = selectPickLists.list();
I wonder how this can be done in the mapping definition.
Thomas