I have a class Parent wich contains an array of 3 Child class objects (Child[] ChildArray). I have a Database Table MyTable with fields [FLD_01], [FLD_02], [FLD_03]. How do I map [FLD_01] to ChildArray[0], [FLD_02] to ChildArray[1] etc?
A:
I would solve it like this:
class Parent
{
public Parent()
{
Children = new Child[3];
}
public Child[] Children { get; private set; }
private Child1 { get { return Child[0]; } set { Child[0] = value; } }
private Child2 { get { return Child[1]; } set { Child[1] = value; } }
private Child3 { get { return Child[2]; } set { Child[2] = value; } }
}
The easily map it as separate properties.
<property name="Child1" />
<property name="Child2" />
<property name="Child3" />
Probably best solution is to just map it to another table.
<array table="Children">
<key name="Parent_FK"/>
<index name="array_index"/>
<composite-element>
<property name="Foo"/>
<property name="Bar"/>
</composite-element>
</array>
Stefan Steinegger
2009-10-07 11:59:30
it would be nice if I had only 3 Child items.. but there are 10 and every Child item contain 5 fields..is there any other solution?
npeBeg
2009-10-07 13:33:07
You always need to specify all the columns you have in the database in your mapping file. If there is 10 times the same 5 fields, you need to map 50 columns, there is no way around. If you want to avoid it, avoid this 50 columns and map the whole stuff into another table.
Stefan Steinegger
2009-10-07 14:24:08