NHibernate - CreateSQLQuery异常“没有persister for ..”,为什么?

时间:2011-10-10 10:38:38

标签: hibernate nhibernate fluent-nhibernate nhibernate-mapping

我正在尝试使用CreateSQLQuery在NHibernate中创建一个SQL查询:

var query = session.CreateSQLQuery(
    "select loss.id as loss.Id, loss.start_date as loss.Date, " + 
    "       (select ... limit 1) as loss.Reserve, " +
    "       (select sum(m.val) ...) as loss.Payment " +
    "from LossData loss .. where ...";

// Commenting or uncommenting these doesn't have any effect either
query.AddScalar("Id", NHibernateUtil.Int32);
query.AddScalar("Date", NHibernateUtil.DateTime);
query.AddScalar("Reserve", NHibernateUtil.Double);
query.AddScalar("Payment", NHibernateUtil.Double);

query.AddEntity("loss", typeof(LossQueryDto));
query.SetResultTransformer(Transformers.AliasToBean(typeof(LossQueryDto)));

var list = query.List<LossQueryDto>();

但是它给了我一个错误“No persister for:'LossQueryDto'”。这个查询出了什么问题? 我是否需要查询的其他映射?如果是这样,我可以在Fluent Nhibernate中定义一个吗?我正在使用NHibernate 3.1。

Dto本身就是这样:

public class LossQueryDto
{
    public virtual int Id { get; set; }
    public virtual DateTime Date { get; set; }
    public virtual double Reserve { get; set; }
    public virtual double Payment { get; set; }
}

谢谢!

1 个答案:

答案 0 :(得分:5)

您不是要查询实体,而是查询DTO,因此您可以删除以下内容:

query.AddEntity("loss", typeof(LossQueryDto));