Saving three related tables using nhibernate
We have three tables A, B, C.
Columns
A => id, x
B => id, y
C => A.id, B.id
For each row in there will be a row in B. For the relationship between A and B, we use C (we cannot change the design of the table). We have to store information in one transaction. Is there a way to do this using NHibernate?
source to share
It depends on the structure of your class or what you are looking for in the class structure. If these are indeed the only columns in the tables, then this seems like overkill and makes for some less optimal ways to access X and Y (classC.AX and classC.BX), but it will work. Perhaps you could have some unsaved properties on ClassC that made these calls for you in a more concise manner.
<class name="ClassA" table="A">
<id name="Id" column="ID">
<generator class="native"/>
</id>
<property name="x" column="X" />
</class>
<class name="ClassB" table="B">
<id name="Id" column="ID">
<generator class="native"/>
</id>
<property name="y" column="Y" />
</class>
<class name="ClassC" table="C">
<id name="Id" column="ID">
<generator class="native"/>
</id>
<many-to-one name="A" class="ClassA" column="A_ID"/>
<many-to-one name="B" class="ClassB" column="B_ID"/>
</class>
If C only has these columns, you can change this to use a composite identifier (see the docs ). Then, also depending on your desires, you can set up the cascading so that you only ever mess up ClassC before CRUD went.
source to share
What you are describing here is a many-to-many table mapping. there is a many-to-many mapping property. this means you don't need a collation for table C.
Borrow match to response from label G.
<class name="ClassA" table="A">
<id name="Id" column="ID">
<generator class="native"/>
</id>
<property name="x" column="X" />
<bag name="List_Of_B" table="C" cascade="all">
<key column="AId"/>
<many-to-many column="BId"
class="B"/>
</bag>
</class>
<class name="ClassB" table="B">
<id name="Id" column="ID">
<generator class="native"/>
</id>
<property name="y" column="Y" />
<bag name="List_Of_A" table="C" cascade="all">
<key column="BId"/>
<many-to-many column="AId"
class="A"/>
</bag>
</class>
source to share