I have a table which PolygonMapping whose register contains a polygon id and other ids of other related tables.
The Polygon table only contains its ID.
There is also the line table, whose register contains the polygon_ID it is part of and a couple of points_Id (start point and end point).
The point table contains 2 coordinates only (X and Y).
I am confused about how to map this database structure using NHibernate for C#. I would like to be able to easily access the lines of a polygon (so I think having a list of lines in the polygon class would be good), and I would like to able to save only the PolygonMapping class when I want to update one point, line or polygon. I would like to have it done automatically.
Please help me!
Thank you.
I would map this problem by creating three domain model objects: a Point object that describes a point, a Line object that contains a two Point objects, one named “StartPoint” and one named “EndPoint”, and a Polygon object that contains an IEnumerable of Lines. The domain objects would look like this:
You could persist this class using a database schema that has a table for each domain model object.

The SQL DDL to create this database structure is as follows:
Your final task is to write your nHibernate mapping file to map the domain model to the underlying database tables. This can be done as shown below. Note that I set the “cascade” attributes to “all” to meet your requirement that saving the parent Polygon object cascades the changes to the child objects.
With this mapping you can manipulate your parent Polygon object and the entire object graph will be persisted to the database when it is saved. For example, to add a new Line to a Polygon object, you can use the following code snippet:
Edit:
The above mapping assumes that you always want to access Line objects only thru the parent Polygon object. If you want to access Lines directly, you may want to add a many-to-one reference from the Line object to the Polygon parent. To do this, you will need to add the following property to the Line class:
as well adding the corresponding mapping in the Line mapping file:
With these changes, you should now be able to directly load a Line object that contains it’s Polygon parent:
Edit 2
Note that if you make the Polygon-Line association bidirectional, you will need to add code to your domain model to ensure graph consistency. See for example this SO post.