Hello
Here is my problem...
Im creating mappings for read only access to legacy database which uses composite keys extensively.
Following schema is used for "connecting" TV_DEVICEs to one TV_UNIT:
Code:
TABLE TV_DEVICE (
HOUSE_ID int not null,
TV_DEVICE_ID int not null,
....some other fields
) PRIMARY KEY (HOUSE_ID, TV_DEVICE_ID)
TABLE TV_UNIT (
HOUSE_ID int not null,
TV_UNIT_ID int not null,
ACTIVE_FROM datetime not null,
TV_DEVICE_ID_1 int,
TV_DEVICE_ID_2 int,
TV_DEVICE_ID_3 int,
) PRIMARY KEY (HOUSE_ID, TV_UNIT_ID, ACYIVE_FROM)
I map this tables like this:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Mandysoft.TvDevice,Mandysoft" table="TV_DEVICE">
<composite-id access="field">
<key-many-to-one name="House" class="Mandysoft.Houses, Mandysoft" column="HOUSE_ID" />
<key-property name="TvDeviceID" column="TV_DEVICE_ID" type="Int32" />
</composite-id>
<property ..........
</class>
</hibernate-mapping>
and :
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Mandysoft.TvUnit,Mandysoft" table="TV_UNIT">
<composite-id access="field">
<key-many-to-one name="House" class="Mandysoft.Houses, Mandysoft" column="HOUSE_ID" />
<key-property name="TvUnitID" column="TV_UNIT_ID" type="Int32" />
<key-property name="ActiveFrom" column="ACTIVE_FROM" type="DateTime"/>
</composite-id>
<many-to-one name="TvDevice1" class="Mandysoft.TvDevice,Mandysoft" insert="false" update="false" >
<column name="HOUSE_ID"" />
<column name="TV_DEVICE_ID_1"/>
</many-to-one>
<many-to-one name="TvDevice2" class="Mandysoft.TvDevice,Mandysoft" insert="false" update="false" >
<column name="HOUSE_ID"" />
<column name="TV_DEVICE_ID_2"/>
<many-to-one name="TvDevice3" class="Mandysoft.TvDevice,Mandysoft" insert="false" update="false" >
<column name="HOUSE_ID"" />
<column name="TV_DEVICE_ID_3"/>
<property .....
</class>
</hibernate-mapping>
Problem is in many-to-one association of TvDevice1,2,3 in TvUnit class- these columns may contain NULLs and in that case i need the references to be null. But thats is the problem because first part of foreign key (HOUSE_ID) is never NULL (it is part of primary key) so when NHibernate loads entity TvUnit with some of TV_DEVICE_ID_x columns null, it then tries to load TvDevice entity using (ID_HOUSE,0) key to TV_DEVICE table (it uses 0 instead of null for second part of key) - and then throws excetion that object cannot be located (of cource)....
Any thoughts ?
Thanks!
Michal
Hibernate version: 1.0.2