I have the following tables:
- Application
- Component
- Position
An Application can have 1 to many Components
An Application can have 1 to many Positions.
When I try to run a join using NHibernate like this:
IEnumerable<Application> applications = Session.Query<Application>()
.FetchMany(r => r.Components)
.FetchMany(r => r.Positions)
and I look at the positions list it has duplicate positions. This goes away if I remove the first FetchMany():
IEnumerable<Application> applications = Session.Query<Application>()
.FetchMany(r=>r.Positions)
I thought NHibernate takes care of normalizing your collections when doing multiple 1 to many joins but it doesn’t seem to be working.
Any suggestions or workarounds on how to allow me to do multiple 1 to many joins using NHibernate LINQ without getting these duplicate records?
This is not a solution, but a workaround that performs better than a solution.
Seperate queries will probably perform better than one big query because they retrieve less data. You can execute the separate queries at once by using
.Future()One large result set with 2 Apps with 2 components and 2 positions = 24 elements
Three small result sets with 2 Apps with 2 components and 2 positions = 10 elements
Select N+1 and outer join are not the only choice.