I'm trying to get a root entity and eager fetch it's child entities. But because I'm using the IStatelessSession of NHibernate, it returns duplicates of the root entity for each child. Using an ISession, it would be solved with
.TransformUsing(new DistinctRootEntityResultTransformer())
But for an IStatelessSession it's not.
Basically it's about the code below, where there's just one instance of Parent, holding 3 Childs.
var result = session.QueryOver<Parent>()
.Fetch(i => i.Childs).Eager();
This will return 3 duplicate instances of Parent, instead of just one. Does anyone have a solution for this?
I would say: Do not use StatelessSession. It does not suite to this use case.
13.2. The StatelessSession interface
I just tried explain that here: NHibernate: Select one to Many Left Join - Take X latest from Parent, The problem here is, that your JOIN is resulting in this SQL result, which is not suitable for paging (which you will need sooner or later)
So this resultset is not the way to go. I would strongly suggest: use
The query could/should be like this:
As the above code snippet shows, to avoid 1 + N issue, we have to use one of the smart mapping features:
19.1.5. Using batch fetching
And the parent mapping should be like:
Please, check also these:
NOTE: Some people could suggest to you to use Result Transforemer, as you've tried. This solution could work, but is done in C#, in memory, so all data are loaded (multi lines) and then narrowed. I would never use that. Check: Criteria.DISTINCT_ROOT_ENTITY vs Projections.distinct