I've to map an existing db with a 'one to one' relation between two tables and composite-keys.
I played around for a few days now, but i can't find any good samples for relations with composite keys and how to deal if there a no null values on no associations.
There are a few thing's i'm not sure, would be a great help, if anybody could share a few sugggestions with me.
first for the 'legacy-tables':
table t_cando
Code:
Id varchar
Part int
..
Date DateTime
Person varchar
table t_todoCode:
Id varchar
...
Date DateTime
Job varchar
...
CanDoId varchar
CanDoPart int
Id and Part together are the primry key for t_cando.
Only Id is the primary key for t_todo.
the system defines a 'job match' by assigning a 'cando' to a 'todo'.
therefore CanDoId and CanDoPart get the aproriate Key-Values from cando-jobs. if there's no association CanDoId is '' and CanDoPart has Value 0.
because i couldn't find the ability with one-to-one and composite-keys in dokumentation, i tryed to map this as follows.
Code:
<class name="CanDo" table="t_cando">
<composite-id name="kCanDo" class="kCanDo">
<key-property name="Id1" type="String" column="Id"/>
<key-property name="Id2" type="Int32" column="Part"/>
</composite-id>
...
<property column="Date" type="DateTime" name="Date" not-null="true"/>
<property column="Person" type="String" name="Person" not-null="true"/>
...
Code:
<class name="ToDo" table="t_todo">
<id name="Id" column="Id" type="String" length="255" />
<many-to-one name="CanDo" class="CanDo">
<column name="CanDoId" />
<column name="CanDoPart" />
</many-to-one>
...
<property column="Date" type="DateTime" name="Date" not-null="true"/>
<property column="Job" type="String" Name="Job" not-null="true"/>
...
because i can't change the column names or the application, i've to live with these things.
For perfomance it's necessary to get all data for assigned and unassigned ToDo's in one query with the CanDo's.
fe. ' from ToDo d where d.Date = '01.09.2005' ' should read all necessary data for CanDos and ToDos (in a outer-join?)
so i've got a few questions:
* Is this mapping appropritate, or are there better solutions for this scenario ? (collections?)
* Ho do the CanDoId and CanDoPart Columns of t_todo find there right parts Id and Part within t_cando ? There seems to be no ability to specify the names of the corrsponding columns within the other table. Do the columns have to be in the same order as the key-property name definitions ? column doesnt't accept something like property name =
SOLVED: the order is as expected, relevant
* the system does'nt use null values for values in CanDoId and CanDoPart. CanDoId is '' and CanDoPart is 0 if there is no 'job assignment'. Can i specify this anywhere ? Doing a scan-query 'from ToDo d where d.Date = '01.09.2005' on gives me somtimes NHibernate.ObjectNotFoundException: No row with the given identifier exists: I think this i because empty assignments don't use null within these fields, so it might try to find a cando where ther is no. but i'm not sure about this. How can i solve this (using a formula translating column value from '' to null ? if yes, does this need to be done on any column of the composite-id ?, trick around with collections ?)