NHibernate - 执行SQL来填充DTO [英] NHibernate - Execute SQL to populate DTO

查看:106
本文介绍了NHibernate - 执行SQL来填充DTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DTO,而不是一个实体,它代表了返回的数据。我有一个DTO,而不是一个实体,它表示返回的数据在哪里执行sprocs比简单和复杂的QueryOver语句。从查询中查询,并希望将查询结果填充到DTO中。我正在使用命名查询和session.GetNamedQuery()来执行查询。


  1. 我是否必须为DTO创建映射文件?

  2. 如果是这样,是否可以让NHibernate / FluentNHibernate知道它不应该为DTO创建表?我的单元测试使用NH的SchemaExport工具删除和创建模式,并且不想为DTO创建表

请注意我不要使用Projections和AliasToBean来投影QueryOver / Linq查询 - 我需要执行存储过程。
$ b $ Cheers


$ p $ public class YourDto
{
public int YourDtoId {get;组; }
public string YourDtoTitle {get;组; }
}

然后

<$ ($code> var result = yourNhSession
.CreateSQLQuery(选择yourColumn1作为YourDtoId,yourColumn2作为yourDtoTitle from YOUR_TABLE)
.SetResultTransformer(Transformers.AliasToBean< YourDto>)
.List< YourDto>();


I have some instances for reporting where executing sprocs is easier and simpler than complicated QueryOver statements.

I have a DTO, not an entity, that represents the data returned from the query and want to populate the results of the query into the DTO. I am using named queries and session.GetNamedQuery() to execute the query.

  1. Do I have to create mapping files for the DTO?
  2. If so, is is possible to let NHibernate/FluentNHibernate know that it should not create tables for the DTO? My units tests drop and create the schema using NH's SchemaExport tool and don't want to create tables for the DTO's

Please note I don't want to project a QueryOver/Linq query using Projections and AliasToBean - I need to execute the stored procedure.

Cheers

解决方案

With CreateSQLQuery, the following would work without any mapping file. Maybe you can give it a try with named queries :

public class YourDto
{
    public int YourDtoId { get; set; }
    public string YourDtoTitle { get; set; }
}

then

var result = yourNhSession
    .CreateSQLQuery("select yourColumn1 as YourDtoId, yourColumn2 as YourDtoTitle from YOUR_TABLE")
    .SetResultTransformer(Transformers.AliasToBean<YourDto>())
    .List<YourDto>();

这篇关于NHibernate - 执行SQL来填充DTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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