是否可以使用NHibernate调用存储过程,该存储过程返回自定义对象而不是域对象? [英] Is it possible to call a stored procedure using NHibernate which returns a custom object instead of domain object?

查看:104
本文介绍了是否可以使用NHibernate调用存储过程,该存储过程返回自定义对象而不是域对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个不返回域对象的存储过程(即,在hbm文件中具有相应sql表映射的对象);但返回自定义对象.

I have several stored procedures that don't return the domain objects (i.e; objects which have corresponding sql table mapping in hbm files); but return the custom objects instead.

我想使用NHibernate调用这些存储过程,并自动用输出填充我的自定义对象,而不是像我使用SqlDataReader那样手动填充它们.

I want to call these stored procedures using NHibernate and fill my custom objects with the output automatically, instead of filling them mannually as I would do if I use a SqlDataReader.

高度赞赏一个例子.

顺便说一句:我使用nhibernate 3.2的新功能通过代码进行映射.

BTW: I use nhibernate 3.2 new feature mapping by code.

推荐答案

也许您可以尝试以下方法(取自 https://stackoverflow.com/a/10513319/1236044 )

Maybe you can try the following (taken from https://stackoverflow.com/a/10513319/1236044 )

它使用CreateSQLQuery.您可以尝试将select...替换为Exec MyStoredProc

It uses CreateSQLQuery. You may try replacing the select... with Exec MyStoredProc

关键是让选择或存储过程返回与您要填充的DTO属性名称相同的列.

The key point is having your select or stored procedure return columns with the same name as the properties of the DTO you are trying to populate.

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调用存储过程,该存储过程返回自定义对象而不是域对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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