NHibernate 投影来检索集合?
我在检索投影中的字符串集合时遇到一些问题: 假设我有以下类,
public class WorkSet {
public Guid Id { get; set; }
public string Title { get; set; }
public ISet<string> PartTitles { get; protected set; }
}
public class Work {
public Guid Id { get; set; }
public WorkSet WorkSet { get; set; }
//a bunch of other properties
}
然后我有一个要检索 WorkSet.Title、WorkSet.PartTitles 和 Id 的工作 id 列表。
我的想法是做这样的事情:
var works = Session.CreateCriteria<Work>()
.Add(Restrictions.In("Id", hitIds))
.CreateAlias("WorkSet", "WorkSet")
.SetProjection(
Projections.ProjectionList()
.Add(Projections.Id())
.Add(Projections.Property("WorkSet.Title"))
.Add(Projections.Property("WorkSet.PartTitles")))
.List();
Id 和 Title 加载得很好,但 PartTitles 返回 null。 请提出建议!
I´m having some trouble retrieving a collection of strings in a projection:
say that I have the following classes
public class WorkSet {
public Guid Id { get; set; }
public string Title { get; set; }
public ISet<string> PartTitles { get; protected set; }
}
public class Work {
public Guid Id { get; set; }
public WorkSet WorkSet { get; set; }
//a bunch of other properties
}
I then have a list of Work ids I want to retrieve WorkSet.Title, WorkSet.PartTitles and Id for.
My tought was to do something like this:
var works = Session.CreateCriteria<Work>()
.Add(Restrictions.In("Id", hitIds))
.CreateAlias("WorkSet", "WorkSet")
.SetProjection(
Projections.ProjectionList()
.Add(Projections.Id())
.Add(Projections.Property("WorkSet.Title"))
.Add(Projections.Property("WorkSet.PartTitles")))
.List();
The Id and Title loads up just fine, but the PartTitles returns null.
Suggestions please!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用标准这可能不起作用。最有可能的是,这是因为该集合无法通过由条件生成的同一 SQL 查询来检索。
我实际上会考虑检索整个对象。这要容易得多,如果不是非常非常特殊的情况,就不值得这么麻烦。 (顺便说一句,检索整个对象可能会更快,因为它们可能已经在缓存中。)通常重要的是查询的数量(当然还有其复杂性),而不是检索的列数。
它可能适用于 HQL,那里有一个
elements
函数。无论如何考虑使用HQL进行静态查询,它更强大。select 子句中允许使用
elements
,但我不知道你会得到什么。您很可能会得到与结果中的 PartTitle 一样多的记录,因为只构建了一条 SQL 语句。This might not work using criteria. Most probably, it is because the set can not be retrieved by the same sql query that is generated by the criteria.
I would actually really consider to retrieve the whole object. It is much easier and if it is not a very very special case, it is not worth the troubles. (By the way, it could be faster to retrieve whole objects, the may be already in the cache.) What usually counts is the number of queries (and its complexity of course), not the number of columns retrieved.
It probably works with HQL, there is a
elements
function there. Consider to use HQL for static queries anyway, it is more powerful.elements
is allowed in the select clause, but I don't know what you'll get. You most probably get as many records as there are PartTitles in the result, because there is only one SQL statement built.