views:

442

answers:

2

i have:

<bag name="Categories" table="CMS_Articles_Categories" lazy="true">
    <key column="article_id"/>
    <many-to-many class="Framework.CMS.Domain.Category, Framework.CMS" column="category_id"/>
</bag>

This is under an Article mapping. An article can be in many categories and of course a category has many articles. When this is lazy=true then its fine. But the thing is that i want to eagerly load all categories. So the questions are:

1) I understand that i can change it to lazy=false but it seems that nhibernate still executes a separate query. Is it sending out the initial query (e.g. to get all articles) and the subsequent one (to get the categories of an article) in batches? cause it doesnt seem so

2) If i wanted to change this mapping to a join, is there any way to make nhibernate load all articles and their categories in one query and construct the objects accordingly? if i change it now to a join it brings back multiple objects for the same id. e.g. if article 5 is in cat 1 and 2 it brings back 5 twice.

A: 

You may want to look at this question. It looks like a similar problem.

g .
+1  A: 

You can do that in 2 ways, one is by setting the fetch option in hbm file, next is you can override the default fetch strategy in Query options.

E.g.

User user = (User) session.CreateCriteria(typeof(User))
            .SetFetchMode("Permissions", FetchMode.Join)
            .Add( Expression.Eq("Id", userId) )
            .UniqueResult();

Refer nhibernate ref docs

Sathish Naga