I am trying to map a table that has a composite key and map another table that references this table.
Assume these tables:
ITEMDELIVERY
with relevant columns:ITEMDELIVERY_ID
DELIVERY_DATE
ITEMDELIVERYDETAIL
with relevant columns:ITEMDELIVERYDETAIL_ID
ITEMDELIVERY_ID
PARTITIONDATE
The columns ITEMDELIVERY.ITEMDELIVERY_ID
and ITEMDELIVERY.DELIVERY_DATE
together form the PK.
The columns ITEMDELIVERYDETAIL.ITEMDELIVERY_ID
and ITEMDELIVERYDETAIL.PARTITIONDATE
form the FK from ITEMDELIVERYDETAIL
to ITEMDELIVERY
.
How do I map this?
I tried the following:
IAutoMappingOverride<ItemDeliveryDetail>
:
mapping.References(x => x.ItemDelivery)
.Columns("ITEMDELIVERY_ID", "PARTITIONDATE");
IAutoMappingOverride<ItemDelivery>
:
mapping.CompositeId().KeyProperty(x => x.Id, "ITEMDELIVERY_ID")
.KeyProperty(x => x.DeliveryDate, "DELIVERY_DATE");
But this doesn't work, it results in a System.InvalidCastException: Invalid cast from 'DateTime' to 'Double'.
when calling SaveOrUpdate
on the session.
UPDATE:
I just checked the generated SQL and it shows that NHibernate somehow switches the values:
INSERT INTO ITEMDELIVERYDETAIL
(ITEMDELIVERYDETAIL_ID, AMOUNT, PROCESSED_BY_REM, SINGLE_ITEM_PRICE,
ITEMDELIVERY_ID, PARTITIONDATE, SupplierInvoice_id)
VALUES (hibernate_sequence.nextval, :p0, :p1, :p2,
:p3, :p4, :p5)
returning ITEMDELIVERYDETAIL_ID into :nhIdOutParam;
:p0 = 20.12.2011 16:29:44 [Type: Double (0)],
:p1 = 6 [Type: DateTime (0)],
:p2 = 21.12.2011 16:29:44 [Type: Double (0)],
:p3 = 7 [Type: Int32 (0)],
:p4 = 0 [Type: DateTime (0)],
:p5 = 19.12.2011 16:29:44 [Type: Int32 (0)],
:nhIdOutParam = 27638398 [Type: Int32 (0)]
As you can see, the parameters are a complete mess...
They should be like this:
:p0 = 6 [Type: Double (0)],
:p1 = 21.12.2011 16:29:44 [Type: DateTime (0)],
:p2 = 7 [Type: Double (0)],
:p3 = 0 [Type: Int32 (0)],
:p4 = 19.12.2011 16:29:44 [Type: DateTime (0)],
:p5 = 27638398 [Type: Int32 (0)],
:nhIdOutParam = NULL [Type: Int32 (0)]