NHibernate投影检索集合? [英] NHibernate Projections to retrieve a Collection?

查看:83
本文介绍了NHibernate投影检索集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在检索投影中的字符串集合时遇到了一些麻烦: 说我有以下课程

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
}

然后,我有一个要检索其WorkSet.Title,WorkSet.PartTitles和ID的工作ID列表.

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();

Id和Title加载得很好,但是PartTitles返回null. 请提出建议!

The Id and Title loads up just fine, but the PartTitles returns null. Suggestions please!

推荐答案

使用条件可能不起作用.很有可能是因为该条件集不能由标准生成的同一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.

它可能与HQL一起使用,那里有一个elements函数.无论如何,考虑将HQL用于静态查询,它会更强大.

It probably works with HQL, there is a elements function there. Consider to use HQL for static queries anyway, it is more powerful.

select 
  ws.Title, 
  elements(ws.PartTitles), 
  w.id
from 
  Work w 
  inner join w.WorkSet ws
where w.id in (:ids)

select子句中允许使用

elements,但是我不知道您会得到什么.您很可能会得到与结果中的PartTitles一样多的记录,因为仅构建了一条SQL语句.

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.

这篇关于NHibernate投影检索集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆