Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

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))
  .SetProjection(Projections.Property("Type"));

DetachedCriteria q = DetachedCriteria.For(typeof(List_Output), "op")
  .CreateCriteria("ProjectTypes")
  .Add(Subqueries.Exists(filter));

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

try{
  result = q.GetExecutableCriteria(s).List();
}finally{
  holder.ReleaseSession(s);
}

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”

Advertisements

Written by Chorn Sokun

April 23, 2008 at 3:22 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: