0

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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much for your speedy response. i solved my problem.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.