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.

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

2 Comments

That did it, thank you. I am not familiar with ToFuture, but I son will be!
Future will batch to selects together and each will initialise one collection. The session cache will correlate the results together and results in both collections initialized
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.