NHibernate - CreateSQLQuery 异常“没有持久化..”,为什么?

发布于 2024-12-09 06:53:49 字数 1173 浏览 0 评论 0原文

我正在尝试使用 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; }
}

谢谢!

I'm trying to create a SQL query in NHibernate using CreateSQLQuery:

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

But it gives me an error "No persister for: 'LossQueryDto'". What's wrong with this query ?
Do I need an additional mapping for the query ? If so, can I define one in Fluent Nhibernate ? I'm using NHibernate 3.1.

The Dto itself is like this:

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; }
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2024-12-16 06:53:49

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

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

You are not querying entities, but DTO, so you can remove following:

query.AddEntity("loss", typeof(LossQueryDto));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文