Partially complete a children's collection with NHibernate

I struggled with this for a while and cannot understand what it is ...

I have a class BlogPostthat has a collection Comments, and each of the comments has a field DatePosted.

I need to make a request for BlogPostand return it with a partially loaded collection Comments, say, all comments posted on August 1, 2009.

I have this query:

BlogPost post = session.CreateCriteria<BlogPost>()
    .Add(Restrictions.Eq("Id", 1))
    .CreateAlias("Comments", "c")
    .Add(Restrictions.Eq("c.DatePosted", new DateTime(2009, 8, 1)))
    .UniqueResult<BlogPost>();

When I run this query and check the generated sql, it first launches the query on the table BlogPost, joining the table Commentwith the correct date limit in, then runs the second query only on Comment, which returns everything.

The result is a complete collection of the Commentsclass BlogPost!

?

, - ...!

+5
5

, . .

:

, Cat, ! , , SetResultTransformer(CriteriaUtil.AliasToEntityMap).

IList cats =
sess.CreateCriteria(typeof(Cat))
    .CreateCriteria("Kittens", "kt")
        .Add( Expression.Eq("Name", "F%") )
    .SetResultTransformer(CriteriaUtil.AliasToEntityMap)
    .List();

, session.EnableFilter(name).

.

+2

- .

BlogPost , Hibernate , , , BlogPost. , . , . , , -.

, , :

List<Comments> comments = session.CreateCriteria<BlogPost>()
            .Add(Restrictions.Eq("Id", 1))
            .CreateAlias("Comments", "c")
            .Add(Restrictions.Eq("c.DatePosted", new DateTime(2009, 8, 1)))
            .list();

. , :

post.setComments(comments); //having already retreived the post elsewhere

, . , .

0

, , , , , , , , !

, , (, , !), , .

Sql Profiler - . , , , , , , - (!), , post.Comments , , !

        var post = session.CreateCriteria<BlogPost>()
            .Add(Restrictions.Eq("Id", 1))
            .UniqueResult<BlogPost>();

        var comments = session.CreateCriteria<Comment>()
            .Add(Restrictions.Eq("BlogPostId", 1))
            .Add(Restrictions.Eq("DatePosted", new DateTime(2009, 8, 1)))
            .List<Comment>();

        post.Comments = comments;

, , post.Comments, ?! :

public class BlogPostMap : ClassMap<BlogPost>
{
    public BlogPostMap()
    {
        Id(b => b.Id);
        Map(b => b.Title);
        Map(b => b.Body);
        HasMany(b => b.Comments).KeyColumnNames.Add("BlogPostId");
    }
}
public class CommentMap : ClassMap<Comment>
{
    public CommentMap()
    {
        Id(c => c.Id);
        Map(c => c.BlogPostId);
        Map(c => c.Text);
        Map(c => c.DatePosted);
    }
}

public class BlogPost
{
    public virtual int Id { get; set; }
    public virtual string Title { get; set; }
    public virtual string Body { get; set; }
    public virtual IList<Comment> Comments { get; set; }
}
public class Comment
{
    public virtual int Id { get; set; }
    public virtual int BlogPostId { get; set; }
    public virtual string Text { get; set; }
    public virtual DateTime DatePosted { get; set; }
}

?

0

, , .

. - :

<query name="loadComments">
<return alias="comments" class="Comment"/>
<load-collection alias="comments" role="Post.comments"/>
from Comments c where c.Id = ? and c.DatePosted = SYSDATE
</query>

, SQL-, . , , . , , .

0

, , BlogPost. .

comments = session.CreateFilter (blogPost.Comments, ...) .List ();
0
source

All Articles