Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7823843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:23:35+00:00 2026-06-02T08:23:35+00:00

I have a table which PolygonMapping whose register contains a polygon id and other

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:23:37+00:00Added an answer on June 2, 2026 at 8:23 am

    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:

    public class Point
    {
        public int Id { get; set; }
        public int XVal {get; set;}
        public int YVal {get; set;}
    }
    
    public class Line
    {
        public int Id {get; set;}
        public Point StartPoint {get; set;}
        public Point EndPoint {get; set;}
    }
    public class Polygon
    {
        public Polygon()
        {
            Lines = new HashSet<Line>();
        }
        public int Id {get; set;}
        public string Description { get; set; }
        public ICollection<Line> Lines { get; set; }
    }
    

    You could persist this class using a database schema that has a table for each domain model object.
    Database Schema

    The SQL DDL to create this database structure is as follows:

    create table Point
    (
        PointId int primary key identity(1, 1),
        XVal int,
        YVal int
    )
    
    create table Polygon
    (
        PolygonId int primary key identity(1, 1),
    [Description] nvarchar(255)
    )
    
    create table Line
    (
        LineId int primary key identity(1, 1),
        PolygonId int foreign key references Polygon(PolygonId),
        StartPointId int foreign key references Point(PointId),
        EndPointId int foreign key references Point(PointId)
    )
    

    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.

      <class name="Polygon" table="Polygon" lazy="false" >
        <id name="Id" column="PolygonId">
          <generator class="identity" />
        </id>
        <property name="Description" column="Description" />
        <set name="Lines" table="Line" lazy="false" cascade="all">
          <key column="PolygonId" />
          <one-to-many class="Line"  />
        </set>
      </class>
    
      <class name="Line" table="Line" lazy="false">
        <id name="Id" column="LineId">
          <generator class="identity" />
        </id>
        <many-to-one name="StartPoint" column="StartPointId" class="Point" cascade="all"/>
        <many-to-one name="EndPoint" column="EndPointId" class="Point" cascade="all"/>
      </class>
    </hibernate-mapping>
    

    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:

            using (var session = factory.OpenSession())
            using(var tran = session.BeginTransaction())
            {
               var newPoint = session.Get<Point>(5);
               var newPoint2 = session.Get<Point>(2);
               var newLine = new Line { StartPoint = newPoint, EndPoint = newPoint2 };
                var foo2 = session.Get<Polygon>(1);
                foo2.Lines.Add(newLine);
                session.SaveOrUpdate(foo2);
                tran.Commit();
             }
    

    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:

     public Polygon Polygon {get; set;}
    

    as well adding the corresponding mapping in the Line mapping file:

    <many-to-one class="Polygon" name="Polygon" lazy="false"  />
    

    With these changes, you should now be able to directly load a Line object that contains it’s Polygon parent:

    var line = session.Get<Line>(5);
    var parent = line.Polygon;
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table which has several one to many relationships with other tables.
I have a table which contains the item and category ids: create table SomeTable
I have a table which is referenced by foreign keys on many other tables.
I have table which contains double values stored in mysql database ...I need to
I have a table which needs to link one of three seperate tables, but
I have a table which contains my ads that can be searched in sql-server-2008.
I have table which contains 2 date fields. Like: id from to --- ----
I have table a which stores the user id and the ids of his
I have a table which contains a bunch of rows and a sort_order .
I have a table which fills out the rows I have put in there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.