1

I have Three tables A, B, and C. A is a parent table with mutiple child records in B and C.

When I query into A I am getting too many records, as though FNH is doing a Cartesian product.

My query is of the form:

var list = session.Query<A>()
  .Fetch(a=> a.Bs)
  .Fetch(a=> a.Cs)

Where Bs is the IList property of A, and Cs is the IList property of A.

I should get only as many Bs as relate to A, and only as many Cs relate to A. Instead I get BxC elements of each.

Is there a better way to load these? I am pretty sure I avoided this exact issue in the past, but don't see it in my old example code.

4

2 回答 2

2

You could use a Transformer to get a distinct result:

var list = session.Query<A>()
  .Fetch(a=> a.Bs)
  .Fetch(a=> a.Cs)
  .SetResultTransformer( Transformers.DistinctRootEntity )

This is NH3.2 syntax, for 2.1 you need to use new DistinctRootEntityTransformer() (I think) as parameter to SetResultTransformer instead.

于 2012-10-24T09:11:14.380 回答
2

I'm not sure if this is a NH bug or a mapping issue, however the query could be optimised to

session.Query<A>()
    .Fetch(a=> a.Bs)
    .ToFuture();

var results = session.Query<A>()
    .Fetch(a=> a.Cs)
    .ToFuture()
    .ToList();
于 2012-10-24T11:21:39.690 回答