1

I am receiving this exception ONLY when I set the property-ref in my xml file.

Initializing[Domain.Entities.R#12345]-failed to lazily initialize a collection of role: Domain.Entities.R.LP, no session or session was closed

LP.hbm.xml
----------
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Domain.Entities" assembly="Domain">

<class name="LP" table="LP">
  <id name="Id">
    <column name="Id" sql-type="int" not-null="true"/>
  </id>

  <property name="AnotherField"/>
  <property name="PaymentDate"/>
  <property name="PaymentAmount"/>
</class>

</hibernate-mapping>


R.hbm.xml
---------
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Domain.Entities" assembly="Domain">
<class name="R" table="R">
<id name="Id">
  <column name="Id" not-null="true"/>
</id>

<property name="AnotherField"/>

<set name="LP">
  <key column="AnotherField" property-ref="AnotherField" />
  <one-to-many class="Domain.Entities.LP" not-found="ignore" />
</set>
</class>
</hibernate-mapping>

        IQueryable<Entities.R> query = _db.Query<Entities.R>();

        var query2 = _db.Query<Entities.LP>().ToList();

        var queryResults = query.ToList();

        Iesi.Collections.Generic.ISet<Entities.LP> lp; 
        try
        {
            lp = queryResults.First().LP;   <--- this fails with exception
        }
        catch (Exception ex) 
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            var lp2 = _db.Query<Entities.LP>().Take(100); <-- works just fine
        }

What I don't understand is why does lp2 get set fine, but lp fails? I know the data model isn't ideal, but it's what I have to work with for now. If I remove the property-ref from the xml file in nhprof I see it make the calls to the SQL table (with the wrong value so I get no data back), but it doesn't fail. This only occurs when I have the property-ref set.

Any help would be greatly appreciated. This is my first run with NH.

ben
  • 21
  • 3

1 Answers1

0

The error message is giving you the answer. Your set of LP objects in the R class is lazy loaded by default. This means you need to access the LP collection within a session.

Typically, you do this by getting an ISessionFactory and calling OpenSession in a using block. Access the collection in the using block and you should be fine.

Colin Grealy
  • 615
  • 4
  • 12
  • Colin thanks for responding. The ISession is created from IoC; what I don't understand is I have a valid ISession in the context of the function I'm in. This works...the ISession is used var lp2 = _db.Query().Take(100); This doesn't work and doesn't have access to ISession. var lp = queryResults.First().LP; So question is...why is one line able to access ISession while the other can't? Thanks again! – ben Oct 07 '13 at 02:28
  • If property-ref is removed it will generate lazy loaded queries without the ISession exception. The ISession exception occurs when property-ref is added. The problem is the data model needs to query by using AnotherField instead of the primary key of the table. – ben Oct 07 '13 at 02:36
  • hmmm, you say this line works var lp2 = _db.Query().Take(100); but that is a LINQ expression that is not evaluated. Have you tried actually accessing the values in lp2 (i.e. iterating over them)? Besides, line queries the LP object. That is not the problem. The problem is accessing the LP collection on the R object. They are two completely different queries, unless I'm reading it wrong. – Colin Grealy Oct 07 '13 at 03:46