Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

Posts Tagged ‘DetachedCriteria

Many to many lazzziii

leave a comment »

I scratched my head for a few hours reading between the line of the following code

DetachedCriteria filter = DetachedCriteria.For(typeof(Project))
  .Add(Expression.Eq("ID", pid))

DetachedCriteria q = DetachedCriteria.For(typeof(List_Output), "op")

ISession s = holder.CreateSession(typeof(List_Output));
IList result;

  result = q.GetExecutableCriteria(s).List();

Wonder why It generate so many sql call to the server? the domain objects in discussion are Project which belong to a Type and base on selected Type a project can choices a bunch of Outputs.
So Project is belong to a Type [One-to-Many], and Type has many Outputs where Output belong to Many Types [Many-to-Many].

After a while I found the issue and got it fixed “it is the Lazzzzzzzziiiiiiiiiiiiiiiines” configuration :))
I forgot to tell Type to load Outputs in a Lazy mode and vise versa, once configured I got only one YEAH ! I mean ONE (a single) sql call to the server instead of 2 + N (where N is number of relevant Outputs in a Type)

Thanks to DetachedCriteria & the Laziness until next time don’t forget the magic word is “Lazy = true”

Written by Chorn Sokun

April 23, 2008 at 3:22 pm