Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to reference ID property in relationship with NHibernate?
    primarykey
    data
    text
    <p>How do I map relationship, where child endpoint is exposed via Id property and not via whole Parent object?</p> <p>Here is the example:</p> <pre><code>class Parent { public Guid Id { get; set; } public List&lt;Child&gt; Chlidren { get; set; } } class Child { public Guid Id { get; set; } public Guid ParentId { get; set; } } </code></pre> <p>Here are the equivalent mappings I'm using:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Blabla" namespace="Blabla" auto-import="false"&gt; &lt;typedef name="ChildrenList" class="Blabla" /&gt; &lt;class name="Parent" table="Parent" lazy="false"&gt; &lt;id name="Id" column="ID" type="Guid"&gt; &lt;generator class="guid" /&gt; &lt;/id&gt; &lt;bag name="Children" table="Child" cascade="save-update" collection-type="ChildrenList" lazy="false"&gt; &lt;key column="ParentID" not-null="true" /&gt; &lt;one-to-many class="Child" /&gt; &lt;/bag&gt; &lt;/class&gt; &lt;class name="Child" table="Child" lazy="false"&gt; &lt;id name="Id" column="ID" type="Guid"&gt; &lt;generator class="guid" /&gt; &lt;/id&gt; &lt;!-- How to map ParentID here? --&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>When I create a parent, add some children to <code>Children</code> collection and then save the parent, everything is fine. But if save a parent object first, then create a child, setting its ParentID property to ID of the parent, then I get</p> <pre><code>NHibernate.PropertyValueException: not-null property references a null or transient value Child._Parent.ChildrenBackref </code></pre> <p>All attempts to map <code>many-to-one</code> relationship resulted in different exceptions while creating NHibernate configuration. Mostly about object type mismatch.</p> <p>I'm sure NHibernate is capable to handle this scenario. There must something fairly basic that I miss.</p> <p><strong>EDIT:</strong></p> <p>I think it make sense to the example test, which fails with above exception:</p> <pre><code>var child = new Child(Create.Saved&lt;Parent&gt;().Id); // this sets the ParentId property this.Repository.Save(child); // here I get the exception </code></pre> <p>My thoughts why NHibernate is raising this: <code>Children</code> property of <code>Parent</code> class mapped in a way that says that a child cannot exist without a parent (<code>&lt;key column="ParentID" not-null="true" /&gt;</code>). When I try to persist a child, NHibernate tries to resolve this relationship (to find a parent this child relates to) and fails, since being given no child endpoint (which otherwise would be <code>ParentId</code> property) in the mapping, it check for its own <code>Child._Parent.ChildrenBackref</code> endpoint, whatever it is.</p> <p>This looks like a desired solution: Mapping <code>ParentId</code> property as child endpoint of the relationship. This would force NHibernate to resolve a parent by using value of <code>ParentId</code> property as parent's primary key.</p> <p>The thing is I don't know if it's possible.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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