Hello,
This might be an obvious question to experienced users, but this is new to me, and I can't seem to find it in the documentation or in the forums.
I have four tables, lets call them A, B, C and X.
X is an association table for a many-to-many relationship between A, B and C.
X has got a column for each association: referenceA, referenceB and referenceC.
A, B and C each have their associated classes ClassA, ClassB and ClassC.
In my *.hbm.xml files, that looks like this:
Code:
<class name="ClassA, assemblyName" table="A">
<id name="Id" column="AID" ...>
<property ... />
...
</class>
<class name="ClassB, assemblyName" table="B">
<id name="Id" column="BID" ...>
<property ... />
...
</class>
<class name="ClassC, assemblyName" table="C">
<id name="Id" column="CID" ...>
<property ... />
...
</class>
I want to be able to do this:
Code:
ClassA a = GetAByWhateverMeans();
IList instancesOfB = a.InstancesOfB;
foreach(ClassB instanceOfB in instancesOfB)
{
Console.Writeline(instanceOfB.Id);
foreach(ClassC instanceOfC in instanceOfB.InstancesOfC)
{
Console.Writeline(instanceOfC.Id);
}
}
But what I want form this instanceOfC is only instances that apply to A!Now of course comes my question: how do I map this association?
In a two way association that's easy:
Code:
<bag name="InstancesOfB" table="X">
<key column="referenceA"/>
<many-to-many class="ClassB, assemblyName" column="referenceB"/>
</bag>
I could declare such an InstancesOfB and InstancesOfC for ClassA. Then a InstancesOfA and InstancesOfC for class B. Finally InstancesOfA and InstancesOfB for ClassC.
Is this the way to go? Is there a better alternative, like creating a ClassX to manage these associations? Do I not run the risk of having circular references?
Any help would be appreciated.
Note:
NHibernate version:1.0.2.0