WoF_Angel
WoF_Angel

Reputation: 2591

C# NHibernate - Accessing an element's list throws error (failed to lazily initialize...)

I'm using MVC2, NHibernate 3.2.

My Classes:

public class NHibernateHelper
{

    private static ISessionFactory _sessionFactory;

    private static ISessionFactory SessionFactory
    {
        get
        {
            if (_sessionFactory == null)
            {
                var configuration = new Configuration();
                configuration.Configure();
                configuration.AddAssembly(typeof(EstoqueEquipamento).Assembly);
                _sessionFactory = configuration.BuildSessionFactory();
            }
            return _sessionFactory;
        }
    }

    public static ISession OpenSession()
    {
        return SessionFactory.OpenSession();
    }
}

I query the element with this:

 public Car GetCar(object pk)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {

                return session.Get<Car>(pk);
            }
        }

The Car mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateClasses" namespace="NHibernateClasses.Entities">
    <class name="Car" table="STACK.CAR" lazy="true" dynamic-update="true" dynamic-insert="true" >
        <id name="CarId" column="CAR_ID" type="Int32" unsaved-value="0">
            <generator class="sequence">
                <param name="sequence">SQ_CAR</param>
            </generator>
        </id>
        <bag name="TireList" inverse="true" generic="true" lazy="true" >
            <key>
                <column name="CarId"/>
            </key>
            <one-to-many class="Tire" />
        </bag>




        <property name="Plate" type="String" column="MAH_PLATE" />



    </class>
</hibernate-mapping>

The Tire mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateClasses" namespace="NHibernateClasses.Entities">
    <class name="Tire" table="STACK.Tire" lazy="true" dynamic-update="true" dynamic-insert="true" >
        <id name="TireId" column="Tire_ID" type="Int32" unsaved-value="0">
            <generator class="sequence">
                <param name="sequence">SQ_Tire</param>
            </generator>
        </id>


        <property name="Brand" type="String" column="MAH_BRAND" />



    </class>
</hibernate-mapping>

Then i query the element:

var car = GetCar(1);//works just fine
var tirelist = car.Tires;//throws error!

The first line works with no problems, but the last line throws the following error:

Initializing[Car]-failed to lazily initialize a collection of role: Car.TireList, no session or session was closed

Let me know if you need the config file also.

Thank you

Upvotes: 0

Views: 1294

Answers (1)

Pieter
Pieter

Reputation: 3399

In GetCar you close the session (due to the using), before you access the Tires-collection later on in your code. Collections are loaded lazily by default in nHibernate, so make sure you fetch the collection before ending the transaction. You could use Eager loading to make sure nHibernate loads the child collection as well.

Upvotes: 3

Related Questions