部分填充儿童收集与NHibernate [英] Partially Populate Child Collection with NHibernate
问题描述
我一直在这个挣扎了一会儿,似乎无法推测出来......
我有一个博文
类,它有评论的集合
,并且每个评论有一个 DatePosted
字段。
我需要做什么是查询博文
和部分装载回吧注释
收集,说张贴在2009年8月1日所有评论
我有此查询:
的博文后= session.CreateCriteria<&博文GT;()
。新增(Restrictions.Eq(ID,1) )
.CreateAlias(注释,C)
。新增(Restrictions.Eq(c.DatePosted,新的日期时间(2009年8,1)))
.UniqueResult< ;博文>();
当我运行此查询,并检查了生成的SQL,它首先违背了搜索博文
表,加入到注释
在正确的日期限制表,然后运行刚上<$ C第二个查询$ C>注释表中返回的一切。
结果是注释
集合在博文
类完全填补了!
我在做什么错了?
我有代码样本,如果有人需要更多的信息...!
有一个结果变压器这一点,看的文档
报价:
请注意,该藏品小猫由
之前两次查询没有
。通过标准的预过滤返回的Cat实例举行
!如果你
要检索只是
匹配标准的小猫,你必须使用
SetResultTransformer(CriteriaUtil.AliasToEntityMap)
。IList的猫=
sess.CreateCriteria(typeof运算(CAT))
.CreateCriteria(小猫,克拉)
。新增(Expression.Eq(姓名,F%))
.SetResultTransformer(CriteriaUtil.AliasToEntityMap)
的.List();
块引用>
您也可以使用开始使用<$ C $激活的过滤器C> session.EnableFilter(名)。
有一个的此处类似的问题。
I've been struggling with this for a while, and can't seem to figure it out...
I've got a
BlogPost
class, which has a collection ofComments
, and each of the comments has aDatePosted
field.What I need to do is query for a
BlogPost
and return it with a partially loadedComments
collection, say all comments posted on the 1 Aug 2009.I've got 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 out the generated sql, it first runs a query against the
BlogPost
table, joining to theComment
table with the correct date restriction in, then runs a second query just on theComment
table that returns everything.Result is the
Comments
collection of theBlogPost
class totally filled up!What am I doing wrong?
I've got code samples if anyone needs more info...!
解决方案There is a result transformer for this, see the documentation.
Quote:
Note that the kittens collections held by the Cat instances returned by the previous two queries are not pre-filtered by the criteria! If you wish to retrieve just the kittens that match the criteria, you must use
SetResultTransformer(CriteriaUtil.AliasToEntityMap)
.IList cats = sess.CreateCriteria(typeof(Cat)) .CreateCriteria("Kittens", "kt") .Add( Expression.Eq("Name", "F%") ) .SetResultTransformer(CriteriaUtil.AliasToEntityMap) .List();
You could also use filters that get activated using
session.EnableFilter(name)
.There is a similar question here.
这篇关于部分填充儿童收集与NHibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!