That results in this:
select tag0_.Id as Id3_,
tag0_.Title as Title3_,
tag0_.Description as Descript3_3_,
tag0_.DateCreated as DateCrea4_3_,
tag0_.DateModified as DateModi5_3_,
tag0_.UserId as UserId3_
from Tags tag0_
left outer join Users user1_
on tag0_.UserId = user1_.Id
where exists (select bookmark3_.Id
from TagsBookmarks bookmarks2_,
Bookmarks bookmark3_
where tag0_.Id = bookmarks2_.TagId
and bookmarks2_.BookmarkId = bookmark3_.Id
and (exists (select tag5_.Id
from TagsBookmarks tags4_,
Tags tag5_
where bookmark3_.Id = tags4_.BookmarkId
and tags4_.TagId = tag5_.Id
and tag5_.Title = 'asp.net' /*
@p0 */
and user1_.Username = 'Mr.nuub'
/* @p1 */)))
This still lazy loads the tags for each bookmark.
Op dinsdag 27 augustus 2013 15:37:51 UTC+2 schreef Gunnar Liljas:
>
> With LINQ:
>
> session.Query<Tag>().Where(t=>t.Bookmarks.Any(b=>b.Tags.Any(t2=>t2.Title==?
> && t.User.Username==?)
>
>
> 2013/8/27 Mr.nuub <[email protected] <javascript:>>
>
>> I am developing a bookmark system in ASP.NET MVC and I use NHibernate
>> to query the database.
>>
>> I have a many to many relationship:
>>
>> - A bookmark can have many tags
>> - A tag can have many bookmarks
>>
>> 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 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; }}
>>
>> I want to get a tag, loop trough its bookmarks, and for each bookmark
>> loop trough its tags. To do this I used this:
>>
>> 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 gives me a tag with its bookmarks. However for each bookmark it is
>> doing another query automatically to obtain its tags (lazy loading?). So if
>> I have 10 bookmarks I get 1 + 10 queries. Is it possible to do this with
>> one or two queries?
>>
>> Example with NHibernate Profiler (3 bookmarks):
>>
>> [image: Example with NHibernate Profiler (3 bookmarks)]
>>
>> --
>> 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] <javascript:>.
>> To post to this group, send email to [email protected]<javascript:>
>> .
>> Visit this group at http://groups.google.com/group/nhusers.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>
--
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.