Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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:</p> <pre><code>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&lt;Line&gt;(); } public int Id {get; set;} public string Description { get; set; } public ICollection&lt;Line&gt; Lines { get; set; } } </code></pre> <p>You could persist this class using a database schema that has a table for each domain model object. <img src="https://i.stack.imgur.com/WSqjC.jpg" alt="Database Schema"></p> <p>The SQL DDL to create this database structure is as follows:</p> <pre><code>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) ) </code></pre> <p>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.</p> <p> </p> <pre><code> &lt;class name="Polygon" table="Polygon" lazy="false" &gt; &lt;id name="Id" column="PolygonId"&gt; &lt;generator class="identity" /&gt; &lt;/id&gt; &lt;property name="Description" column="Description" /&gt; &lt;set name="Lines" table="Line" lazy="false" cascade="all"&gt; &lt;key column="PolygonId" /&gt; &lt;one-to-many class="Line" /&gt; &lt;/set&gt; &lt;/class&gt; &lt;class name="Line" table="Line" lazy="false"&gt; &lt;id name="Id" column="LineId"&gt; &lt;generator class="identity" /&gt; &lt;/id&gt; &lt;many-to-one name="StartPoint" column="StartPointId" class="Point" cascade="all"/&gt; &lt;many-to-one name="EndPoint" column="EndPointId" class="Point" cascade="all"/&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>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:</p> <pre><code> using (var session = factory.OpenSession()) using(var tran = session.BeginTransaction()) { var newPoint = session.Get&lt;Point&gt;(5); var newPoint2 = session.Get&lt;Point&gt;(2); var newLine = new Line { StartPoint = newPoint, EndPoint = newPoint2 }; var foo2 = session.Get&lt;Polygon&gt;(1); foo2.Lines.Add(newLine); session.SaveOrUpdate(foo2); tran.Commit(); } </code></pre> <p><strong>Edit:</strong> 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:</p> <pre><code> public Polygon Polygon {get; set;} </code></pre> <p>as well adding the corresponding mapping in the Line mapping file:</p> <pre><code>&lt;many-to-one class="Polygon" name="Polygon" lazy="false" /&gt; </code></pre> <p>With these changes, you should now be able to directly load a Line object that contains it's Polygon parent:</p> <pre><code>var line = session.Get&lt;Line&gt;(5); var parent = line.Polygon; </code></pre> <p><strong>Edit 2</strong> 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 <a href="https://stackoverflow.com/questions/950776/maintaining-a-two-way-relationship-between-classes">this SO post</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload