creating class for stored procedure, returning data from multiple tables

喜夏-厌秋 提交于 2019-12-25 03:49:28

问题


I have an stored procedure returning data from multiple tables. in nhibernate we create class for each table. is it required to create class for each table it is returning and then relating that class with each other. is there any other way of doing it like creating a class that contain all the fields returned by stored procedure

thanks


回答1:


I haven't worked with stored procedures, but you can definitely create a class that maps to a view and not just to a table.

By the way, if you're using QueryOver, you can use the Select method to return custom objects: http://nhforge.org/blogs/nhibernate/archive/2009/12/17/queryover-in-nh-3-0.aspx#Projections




回答2:


This is quite easy to achieve. In your mappings add this:-

<sql-query name="GetItemDTO">
  <![CDATA[exec uspGetSomeResults :id]]>
</sql-query>

Create a class:-

public class ItemDTO 
{
  public virtual long Id { get; protected set; }
  public virtual string Name { get; protected set; }
}

and to retrieve the results

return Session
    .GetNamedQuery("GetItemDTO")
    .SetInt64("id", 123456)
    .SetResultTransformer(new AliasToBeanResultTransformer(typeof(ItemDTO)))
    .List<ItemDTO>();

This assumes that the SP returns an Id and a Name column. These must match perfectly with your class names.



来源:https://stackoverflow.com/questions/5576242/creating-class-for-stored-procedure-returning-data-from-multiple-tables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!