2

I have a nhibernate 3.2 query which returns the first 500 Intervention items (in the original query there is a filter)

var (from interv in Session.Query<Intervention>()
                .Fetch(rep => rep.ReponsePointVerification)
                orderby interv.DateModification
                select interv)
                .Take(500)
                .ToList();

Then I iterate on all value and use ReponsePointVerification value.

// a (very) simplified example
foreach (var intervention in listeInterventions)
  {
  foreach (var reponse in intervention.ReponsePointVerification)
    {

    }

  listeInterventionsws.Add(interventionws);
}

This query is optimized but it doesn't work well because the Take method will take 500 lines and if there are ReponsePointVerification value, I won't have my 500 Intervention items.

So if I want to make it work, I have 2 methods :

  • remove Take(500) and take only the first 500 interventions (heavy when the database grow)
  • Remove the fetch but if I will have 500 + 1 query.

Does nhibernate have a method to handle that case ?

Regards

Edit

Thank you Diego, it worked.

Well I forgot to mention that I used mapping by code in nh 3.2

The batch size with mapping by code can be configured like this :

Bag(x => x.ReponsePointVerification, map =>
{
  map.Key( k => k.Column( "IdIntervention" ) );
  map.BatchSize(50);
}, rm => rm.OneToMany()); 

cf. http://puredotnetcoder.blogspot.com/2011/07/mapping-conventions-with-mapping-by.html

1 Answer 1

1

Use batch-size on the mapping of the affected collections instead of Fetch.

It's a better solution in 90% of the cases.

(I was going to link to the relevant docs section, but the site is down at the moment)

Sign up to request clarification or add additional context in comments.

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.