creating class for stored procedure, returning data from multiple tables
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 cl开发者_开发百科ass that contain all the fields returned by stored procedure
thanks
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.
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
精彩评论