I'm not 100% certain, but my understanding would be that:

1) When performing your 'from Employee e where e.Salary > 100' query,
nhibernate will look to see what fetching strategy you have set for
that query for the e.Country attribute.

If you have the strategy at lazy (Or Select, same thing) (default),
then no join will be done, and when you try and access the e.Country
attribute, I assume that:
a) If the country (via Id) has been previously loaded directly via
session.Get(id), which uses the 1st level (session) cache, then the
Country will be loaded via the 1st level cache
b) If the country has not previously been loaded directly via
session.Get(id), then a single (SELECT N+1) query will be issued to
resolve this property. If you touched 10 e.Country values for 10
different users who had 10 different values, thats 10 selects

2) You could set the fetch strategy to Eager (or Join, same thing) for
the e.Country property, then your HQL query will have a join occur and
pre-load the attribute value for you

3) My understanding of the 2nd level cache is that it caches specific
queries: You'd need to tell (via SetCachable(true) on the HQL call)
nhibernate to cache the query (for future executions, the first time
its executed will hit the db).

I recommend reading 
http://stackoverflow.com/questions/337072/what-is-first-and-second-level-caching-in-hibernate
and 
http://nhforge.org/blogs/nhibernate/archive/2009/02/09/quickly-setting-up-and-using-nhibernate-s-second-level-cache.aspx

Finally, you should just test this.

Turn on show_sql in the config file and check to see what queries get
issued so you can better confirm for yourself how things are working -
that would be what I would do.

- Andrew

On Jun 7, 6:03 pm, Shadi Mari <[email protected]> wrote:
> anybody knows the answer for such scenario?
>
>
>
>
>
>
>
> On Mon, Jun 6, 2011 at 2:27 PM, Shadi Mari <[email protected]> wrote:
> > Hello,
>
> > I have the following domain definitions:
>
> > class Lookup<T>
> > {
> > int Id;
> > public string Name  { get { return based on culture; }}
> > private string Name_en;
> > private string Name_fr;
> > }
>
> > class Country : Lookup<Country>
> > {
> > }
>
> > class Employee
> > {
> > int Id;
> > float Salray;
> > Country Country;
> >  }
>
> > I also have a db table for Employees(Id, Salary, CountryId) and another
> > table from Countries (Id, Name_en, Name_fr).
>
> > I would like to benefit from NH 2nd level cache for Lookups (i.e.
> > Countries), yet am not sure how NH behaves when executing the following HQL
> > query "from Employee e where e.Salary > 100".
>
> > Does NH in this case checks the 2nd level cache to find Countries with
> > associated Ids or it does produces a SQL query which joins the Employees db
> > table with Countries db table every time the HQL query is executed, if not
> > then what value 2nd level cache adds in this case.
>
> > Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to