Free issues with NHibernate LazyLoad

I could not find the answer to this question, so I assume that this is what I am doing wrong.

I have a PersistenceModel where I set the convention as follows: -

persistenceModel.Conventions.Add(DefaultLazy.Always()); 

However, for one of HasManyToMany relationships in one of my objects, I want the download to load, which I configure as follows: -

 HasManyToMany(x => x.Affiliates).Not.LazyLoad(); 

Intuitively, I expect an intensive load to happen, as I redefine the value of the lazy load, which I indicated as convention, but still lazy loads. If I set the DefaultLazy agreement and never installed LazyLoad in separate respects, it doesn't work either.

Any ideas?

+6
nhibernate lazy-loading
source share
2 answers

When you install Not.LazyLoad (), you tell NHibernate to load Affiliates when the parent loads. NHibernate will do this by making a different choice in the Affiliates many-to-many table, whether you are accessing the Affiliates collection or not. NHibernate uses a different selection because it is the default fetch mode. You also want to override the boot mode, both in the request and in the mapping. To do this in the mapping, add the following:

 HasManyToMany(x => x.Affiliates) .Not.LazyLoad() .Fetch.Join(); 

You can also include ".Cascade.AllDeleteOrphan ()" if you want NHibernate to keep new Affiliaites added to the collection and removing the orphans. If you do not, you will have to explicitly call session.Save (newAffiliate). Otherwise, you will get a TransientObjectException when your Affiliates collection contains a new partner.

+9
source share

This may be one stupid thing, but are you doing the query inside your session? Let's say

 Using(var session = OpenSession()) { session.Query<Entity>().ToList(); } 

I had this problem before, and finally I realized that the objects I was accessing were not requested before deleting the session.

0
source share

All Articles