R – NHibernate and lazy loading: how to get only few rows from the collection

nhibernate

When using NHibernate and lazy loading, how would I get this kind of behavior:

I have a Customer class, and the customer has many addresses (say 100 to make this make more sense).

I load my customer row, I want to just access 3 items from the addresses collection.

I don't want nHibernate to load all 100 addresses, but with lazy loading from what I understand, will load all of them?

I want 3 of them only, and I want to load all 3 at once, how would I get this behavior?

Best Answer

It depends on

  • if you need "just one" or a certain one
  • if you need the other items in the list later in the same session

You can use

  • filters to filter the collection. These filters are defined in the mapping file and activated and parameterized when using the session
  • AliasToEntityMap result transformer which lets you filter the collection within a query
  • batch fetching that fetches a certain amount of elements at once.
Related Topic