koala
koala

Reputation: 1564

Nhibernate LazyInitializationException after Session.Clear

In my application before every query I want to do the following

public List<Supplier> GetAllSuppliers()
{
        NHibernateHelper.Session.Clear();

        string query = "from Supplier order by FirstName, LastName asc ";

        var result = NHibernateHelper.Session.CreateQuery(query).List<Supplier>();

        return (List<Supplier>)result ?? new List<Supplier>();
}

The following code line is meant to clear the session so when I query all my suppliers I want to have the whole list again from the database and not from the cache. Because NHibernate remembers that object or something like that and does not receive the changes that are made in the database (my application is used on multiple computers, that's the main reason I want to get the "live" data)

NHibernateHelper.Session.Clear();

But my Supplier object has an Address object which is being lazy loaded. In my list I select that Supplier object and it shows it's name and also the address. And that's when the errors occurs:

An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: Initializing[Model.Supplier#1]-Could not initialize proxy - no Session.

And if set that line of code in comments It works...But I don't want that behaviour that it uses the cache...

Mapping for Supplier

    public SupplierMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Btw);
        Map(x => x.Email);
        Map(x => x.Fax);
        Map(x => x.Telephone);           

        References(x => x.Address).Cascade.All();

        HasMany(x => x.Articles);

        Table("tbl_suppliers");
    }

Mapping for address

    public AddressMap()
    {
        Id(x => x.Id);
        Map(x => x.Street).Length(10000);
        Map(x => x.Number).Length(10000);
        Map(x => x.Zip).Length(10000);
        Map(x => x.City).Length(10000);
        Map(x => x.Country).Length(10000);

        Table("tbl_addresses");
    }

Any ideas on this issue?

EDIT I've found following link: https://nhibernate.jira.com/browse/NH-2791 Seems like the same problem but with no solution...

Upvotes: 2

Views: 1614

Answers (1)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123901

I believe, that you've tried to find the answer, that you've searched a lot. And the reason why you did not find it, is because you are trying to do something very unusal.

The Session (ISession) abstraction is there for purpose, it is bringing lot of advantages. If we do not like to profit from Session, we can use StatelessSession approach, but mostly we need the Session concept.

In fact, the real issue I see, is hidden in the tag "WPF". You are trying to use NHibernate together with WPF, and the Session seems to be running long time... then it can return stale data...

The solution should be in correct WPF/NHibernate Session lifetime setting and probably the best source for you is here:

Building a Desktop To-Do Application with NHibernate

Upvotes: 1

Related Questions