Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating and retrieving associated mappings in NHibernate
    primarykey
    data
    text
    <p>Say I have two persistent classes <code>Cat</code> and <code>Hat</code>. Let's imagine that the relationship between the two is bidirectionally one-to-many, as in a Cat can have many different Hats, and we can traverse the mapping in both directions. In order to create an association between an instance of each class, I should be able to, and correct me if I'm wrong, do the following:</p> <pre><code>Cat whiskers = new Cat(); Hat redHat = new Hat(); redHat.Id = 1; // normally assigned by generator but this makes example more explicit whiskers.Hats.Add(redHat); catService.Save(whiskers); hatService.Save(redHat); </code></pre> <p>With that in mind, should I not be able to retrieve the Cat which is associated with redHat by doing the following?</p> <pre><code>Hat hat = hatService.FindById(1); // redHat has Id 1 Cat cat = hat.Cat; // cat should be whiskers </code></pre> <p>Unfortunately, when I try to use this technique, cat results as a null object in that last line. I'm guessing I am missing something in my one-to-many mapping in Cat.hbm.xml, or at least I hope it's something that simple. Here's what it currently looks like:</p> <pre><code>&lt;set name="Hats" inverse="true"&gt; &lt;key column="Id"/&gt; &lt;one-to-many class="Hat"/&gt; &lt;/set&gt; </code></pre> <p>I have no mapping in Hat.hbm.xml since adding one resulted in a "duplicate mapping" error. I read the Hibernate <a href="http://docs.jboss.org/hibernate/core/3.3/reference/en/html/associations.html#assoc-bidirectional-m21" rel="nofollow">documentation</a> on association mappings, but still can't get this working. What am I doing wrong?</p> <p>Complete mapping files below:</p> <p><strong>Cat.hbm.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Domain" namespace="Domain"&gt; &lt;class name="Cat" table="cats" lazy="true"&gt; &lt;id name="Id"&gt; &lt;generator class="increment" /&gt; &lt;/id&gt; &lt;property name="Name" /&gt; &lt;set name="Hats" inverse="true"&gt; &lt;key column="Id"/&gt; &lt;one-to-many class="Hat"/&gt; &lt;/set&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p><strong>Hat.hbm.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Domain" namespace="Domain"&gt; &lt;class name="Hat" table="hats" lazy="true"&gt; &lt;id name="Id"&gt; &lt;generator class="increment" /&gt; &lt;/id&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p><strong>Edit</strong></p> <p>I believe the problem was that I was not initializing the <code>ISet&lt;Hat&gt;</code> value in the Cat class. However, I've run into a new problem. When I try to access the associations, I get this error:</p> <p><code>Could not initialize proxy - no Session.</code></p> <p>I figured this would be easily avoided if I created a method in my DAO where I could wrap it in a session like this:</p> <pre><code>public Cat GetRelatedCat(Hat hat) { Cat cat; using (ISession session = HibernateUtil.GetSessionFactory().OpenSession()) { cat = hat.Cat; } return cat; } </code></pre> <p>This yields the same "no session" error. I'm stumped.</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.
 

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