Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate - one-to-one Cascade Settings
    primarykey
    data
    text
    <p>I have a one-to-one relationship between a Person class and an Employee. I expect the INSERT to cascade from the Person to the Employee. However, this does not happen. I've tried cascade='all' and cascade='save-update' on one-to-one relationship element, but it didn't work. </p> <p>The structures of the my objects are as follows:</p> <pre><code>public class Person { public virtual Employee Employee { get; set; } public virtual int Age { get; set; } public virtual string Forename { get; set; } public virtual string Surname { get; set; } public virtual int PersonID { get; set; } } public class Employee { public virtual int PersonID { get; set; } public virtual string PayRollNo { get; set; } public virtual int Holidays { get; set; } public virtual Person Person { get; set; } } </code></pre> <p>Mapping files shown below:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"&gt; &lt;class name="Employee, Employee.DAL" table="`Employee`" &gt; &lt;id name="PersonID" column="`PersonId`" type="int"&gt; &lt;generator class="native" /&gt; &lt;/id&gt; &lt;property type="string" length="30" name="PayRollNo" column="`PayRollNo`" /&gt; &lt;property type="int" name="Holidays" column="`Holidays`" /&gt; &lt;one-to-one name="Person" class="Person" cascade="all"/&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; &lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"&gt; &lt;class name="Person, Employee.DAL" table="`Person`" &gt; &lt;id name="PersonID" column="`PersonId`"&gt; &lt;generator class="foreign"&gt; &lt;param name="property" &gt;Employee&lt;/param&gt; &lt;/generator&gt; &lt;/id&gt; &lt;property type="string" name="Forename" column="`Forename`" /&gt; &lt;property type="string" name="Surname" column="`Surname`" /&gt; &lt;property type="int" name="Age" column="`Age`" /&gt; &lt;one-to-one name="Employee" class="Employee" constrained="true" /&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>Code for initiating the objects and saving them:</p> <pre><code>var employee = new Employee(); employee.Person = new Person { Employee = employee }; ISessionFactory sessionFactory = (new Configuration()).Configure().BuildSessionFactory(); employee.Person.Age = 27; employee.Person.Forename = "N"; employee.Person.Surname = "M"; employee.PayRollNo = "12"; employee.Holidays = 27; using (var session = sessionFactory.OpenSession()) { session.Save(employee); } </code></pre>
    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. 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