Hello,

I have a many to many relation with these models:

public class Bookmark{
    public virtual string Title { get; set; }
    public virtual string Link { get; set; }
    public virtual User User { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }

    public Bookmark()
    {
        Tags = new List<Tag>();
    }}
public class Tag{
    public virtual string Title { get; set; }
    public virtual string Description { get; set; }
    public virtual User User { get; set; }
    public virtual ICollection<Bookmark> Bookmarks { get; set; }

    public Tag()
    {
        Bookmarks = new List<Bookmark>();
    }}

I need to load a specific tag for example: c# for user: Mr.nuub

In my view I use the tag title and description followed by all bookmarks 
that this tag contains. For each bookmark I also need all tags that belongs 
to it.

I used the following criteria:

public Tag GetTagByTitle(string username, string title){
    ICriteria criteriaQuery = SessionFactory.GetCurrentSession()
            .CreateCriteria(typeof(Tag))
            .SetFetchMode("Bookmarks", FetchMode.Eager)
            .CreateAlias("User", "User")
            .Add(Restrictions.Eq("Title", title))
            .Add(Restrictions.Eq("User.Username", username));

    IList<Tag> tags = criteriaQuery.List<Tag>();
    Tag tag = tags.FirstOrDefault();

    return tag;}

This results in a join that returns a tag with its bookmarks. However for 
each bookmark NHibernate creates a new query to obtain its tags. I want one 
single query so I changed my criteria to this:

public Tag GetTagByTitle(string username, string title){
    ICriteria criteriaQuery = SessionFactory.GetCurrentSession()
            .CreateCriteria(typeof(Tag))
            .SetFetchMode("Bookmarks", FetchMode.Eager)
            .SetFetchMode("Bookmarks.Tags", FetchMode.Eager)
            .CreateAlias("User", "User")
            .Add(Restrictions.Eq("Title", title))
            .Add(Restrictions.Eq("User.Username", username))
            .SetResultTransformer(Transformers.DistinctRootEntity);

    IList<Tag> tags = criteriaQuery.List<Tag>();
    Tag tag = tags.FirstOrDefault();

    return tag;}

I mapped the many to many as a set so this works fine. However NHibernate 
Profiler tells me that I used too many joins and this might be a 
performance problem. When is this becoming a performance problem and is 
there a better way to do this? Is perhaps HQL better suited for this?

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to