I just started using Hibernate 3.2.4.sp1 (coming with jboss-4.2.3.GA) trying to map a legacy DB.
I have a problem mapping a non-primary key for an optional join.
Entity Tm has Primary key id, but also a composite business key (mpk1, mpk2), wrapped via class TmPK
There is a one-to-optional-one relation to a second table Td, whose columns are mapped into Tm. But the primary key of Td is not Tm.id, but the composite business key
Code:
class Tm {
Integer id;
TmPK tmPK;
String something;
String other; //from Td
}
Existing queries look something like that:
Code:
SELECT Tm.id, Tm.mpk1, Tm.mpk2, Tm.something, Td.other
FROM Tm
LEFT JOIN Td ON Tm.mpk1 = Td.dpk1 AND tm.mpk2 = Td.dpk2
WHERE Tm.id = ?
Mapping:
Code:
<class name="Tm">
<id name="id"/>
<component name="tmPK" unique="true">
<property name="mpk1">
<property name="mpk2">
</component>
<property name="something"/>
<join table="Td" optional="true">
<key property-ref="tmPK" unique="true" not-null="true">
<column name="dpk1">
<column name="dpk2">
</key>
<property name="other"/>
</join>
even though giving property-ref="tmPK", I get the following Exception:
org.hibernate.MappingException: Foreign key (tmPK:Td [dpk1,dpk2])) must have same number of columns as the referenced primary key (Tm [id])
tried with foreign-key="tmPK", but the same.
Could anybody shed some light on how to map it properly?
thx, Jürgen