问题
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