Skip to main content

NHibernate eager fetching

NHibernate supports the concept of eager fetching for a long time. However there are some things to consider when you start using this with the out-of-the-box NHibernate Linq provider.
Let’s first look at the obvious way:
var customers = session
.Query<Customer>()
.Fetch(c => c.Orders)
.ToList();

This will return the customer and all the customer’s orders in a single SQL statement.
Rule #1: Fetch() statements must always come last.
If you want to mix Fetch with other clauses, Fetch must always come last. The following statement will throw an exception:
var customers = session
.Query<Customer>()
.Fetch(c => c.Orders)
.Where(c => c.CustomerId == "ABC")
.ToList();

But this will work fine:

var customers = session
.Query<Customer>()
.Where(c => c.CustomerId == "ABC")
.Fetch(c => c.Orders)
.ToList();

Rule #2: Don’t fetch multiple collection properties at the same time.
Be careful not to eagerly fetch multiple collection properties at the same time. The following statement will execute a Cartesian product query against the database, so the total number of rows returned will be the total Subordinates times the total orders.

var employees = session
.Query<Employee>()
.Fetch(e => e.Subordinates)
.Fetch(e => e.Orders)
.ToList();

Rule #3: Fetch grandchild collections using FetchMany.
You can fetch grandchild collections too.  The following statement will throw an exception:

var customers = session
.Query<Customer>()
.Fetch(c => c.Orders)
.Fetch(c => c.Orders.OrderLines)
.ToList();

But if you use ‘FetchMany’ and ‘ThenFetchMany’ it will work fine:

var customers = session
.Query<Customer>()
.FetchMany(c => c.Orders)
.ThenFetchMany(o => o.OrderLines)
.ToList();

Popular posts from this blog

Podman– Command execution failed with exit code 125

After updating WSL on one of the developer machines, Podman failed to work. When we took a look through Podman Desktop, we noticed that Podman had stopped running and returned the following error message: Error: Command execution failed with exit code 125 Here are the steps we tried to fix the issue: We started by running podman info to get some extra details on what could be wrong: >podman info OS: windows/amd64 provider: wsl version: 5.3.1 Cannot connect to Podman. Please verify your connection to the Linux system using `podman system connection list`, or try `podman machine init` and `podman machine start` to manage a new Linux VM Error: unable to connect to Podman socket: failed to connect: dial tcp 127.0.0.1:2655: connectex: No connection could be made because the target machine actively refused it. That makes sense as the podman VM was not running. Let’s check the VM: >podman machine list NAME         ...

Azure DevOps/ GitHub emoji

I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.

Cleaner switch expressions with pattern matching in C#

Ever find yourself mapping multiple string values to the same result? Being a C# developer for a long time, I sometimes forget that the C# has evolved so I still dare to chain case labels or reach for a dictionary. Of course with pattern matching this is no longer necessary. With pattern matching, you can express things inline, declaratively, and with zero repetition. A small example I was working on a small script that should invoke different actions depending on the environment. As our developers were using different variations for the same environment e.g.  "tst" alongside "test" , "prd" alongside "prod" .  We asked to streamline this a long time ago, but as these things happen, we still see variations in the wild. This brought me to the following code that is a perfect example for pattern matching: The or keyword here is a logical pattern combinator , not a boolean operator. It matches if either of the specified pattern...