views:

508

answers:

1

This question might be a duplicate of this one:

http://stackoverflow.com/questions/217761/nhibernate-disable-automatic-lazy-loading-of-child-records-for-one-to-many-rela

I'd like to know if there is any way to tell nhibernate to do not load a child collections (best if it's with fluent Nhibernate) unless i do it manually with a query (keeping all the mappings!).

The problem is that even turning off lazy loading the collections get eager-loaded automatically. I'd like that no collections are loaded unless I specify a fetchmode in my query.

A: 

As you can see from the question and answer you linked, you can sort of achieve this by making relationships uni-directional. So instead of directly answering how, I'm going to ask why in hopes of getting to a better "how."

I definitely see why you would not always want collections to be eagerly fetched, and that is why NHibernate has lazy loading (and why NHibernate always lazy loads collections unless you pre-fetch). NHibernate will not actually fetch that collection until you actually ask for it. So my question is why do you want NHibernate to not load the collection when you are operating upon it? What do you want NHibernate to do when you access a child collection on your entity?

Update:

You didn't really answer my question of why (maybe I'm just being stubborn) but fine, you want control. Well the simplest way to keep NH from doing its job is to not tell it to do so in the first place (i.e. don't map it). That is, don't map the "many" (collection) end of the relationship. You can still keep the collection on your class but as long as you haven't told NHibernate to populate it by adding it to your mapping file, it never will. Then you can add a PopulateCollection method somewhere to run the NH query and store the results in that collection. You lose all the nice semantics that NHibernate provides for collections though, because it can't track changes to it; you'll have to implement your own Add and Remove methods.

So, basically David Kemp's answer on the other question.

Stuart Childs
the reason is simple, i want to control manually what gets loaded and what not. Don't want to have queries going to the database under the hood. Also, it's very annoying to have entities being lazy loaded while i'm debugging.I'd expect a null exception if i'm operating on a not loaded collection.
Drevak