igorti
igorti

Reputation: 3856

Fluent NHibernate - avoid loading children collection

I have 2 classes with HasMany relationship, say a class called Parent that has a collection of Children. I want to be able to build queries in NHibernate that would only return Parents that have Children. I guess that to do that I need to have this HasMany relationship.

What I want though is following - when I load Parents, I don't want it's children to be loaded. Kind of LazyLoading, but without loading Children at all.

Any ideas how to accomplish this?

Upvotes: 0

Views: 451

Answers (1)

Firo
Firo

Reputation: 30813

NHibernate's LazyLoading of collections is exactly what you asked for. The collection-object itself is created but none of the child objects is loaded.

When JSON method is called Children objects are loaded lazily. I want to avoid it.

3 Options come to mind

  1. projecting the Parentobject into a DTO which is then serialized
  2. Customizing the JSON serialization
  3. detaching the parent from the session (Evict()) and setting the collection to null before serializing

i would favor option 1, but option 2 would be ok too. Option 3 would be a hack and should be avoided

Upvotes: 3

Related Questions