1

I'm confused by the following NHibernate behaviour:
Domain classes and mappings:

public class Category
{
    public virtual int ID { get; set; }
    public virtual string Name { get; set; }

    private IList<Product> _products;
    public virtual IList<Product> Products
    {
        get { return new List<Product>(_products).AsReadOnly(); }
    }

    public Category()
    {
        _products = new List<Product>();
    }
}
public class Product
{
    public virtual int ID { get; set; }
    public virtual string Name { get; set; }
    public virtual Category Category { get;set; }
}
public class ProductMap : ClassMap<Product>
{
    public ProductMap()
    {
        Schema("dbo");
        Table("tProducts");

        Id(x => x.ID);
        Map(x => x.Name);
        References(x => x.Category).Column("CategoryID");
    }
}
public class CategoryMap : ClassMap<Category>
{
    public CategoryMap()
    {
        Schema("dbo");
        Table("tCategories");

        Id(x => x.ID);
        Map(x => x.Name);
        HasMany(x => x.Products)
            .KeyColumn("CategoryID")
            .Access.CamelCaseField(Prefix.Underscore)
            .Inverse()
            .Cascade.All();
    }
}

Code causing trouble:

    Category category;

    using (var session = sessionFactory.OpenSession())
    {
        category = session.Get<Category>(1);
    }

    using (var session = sessionFactory.OpenSession())
    {
        var products = category.Products; // exception
    }

Why do I get no session exception when I'm trying to get products? I've got a session here! How to avoid this exception (I prefer 2 sessions here, and I want to keep loading lazy)?
Thanks in advance!

3
  • 1
    The session that you used to retrieve the cathegory object has been closed, so whne you try to access the products property, it can't get to it. You may be able to detach the catheogry object from its original session and attach it to the new session that you have opened, but I don't think it seems like a good idea. Why do you want to use a different session? Commented Apr 18, 2011 at 12:41
  • This example is significantly simplified just to show what the problem is. In real application I have several functions to retrive objects. I just don't want to pass session to each of them. Commented Apr 18, 2011 at 12:45
  • 1
    Shouldn´t it be "category"? ;) Commented Apr 18, 2011 at 13:00

2 Answers 2

4

Can you re-attach the object to your new session either:

ISession.Update(myDetachedInstance);  

or, if the object has not been changed:

ISession.Lock(myDetachedInstance, NHibernate.LockMode.None);  

For more info, see: http://intellect.dk/post/Detached-objects-in-nHibernate-and-Lazy-loading.aspx

Sign up to request clarification or add additional context in comments.

Comments

1

I got this same error and it was due to the WPF UI holding on to references to objects from the old nhibernate session after I clicked the "refresh" button which called refresh on the data context which disassociated all cached objects in the session.

I needed to make sure "NotifyChange" was being fired to make the UI update (and that the UI was listening to it).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.