I have a stored procedure that logs some data, how can I call this with NHibernate?
So far I have:
ISession session = ....
IQuery query = session.Cre
NHibernate allows you to do object-oriented programming and takes care of fetching the objects from and saving the objects to the database behind the scenes.
NHibernate does not provide you with an easy API for simply executing stored procedures, because that doesn't seem to have much to do with object-oriented programming, whether fetching objects or saving them.
So you are doing something fundamentally wrong in attempting to use NHibernate directly to execute highly procedural code. If you want to use NHibernate, you have to tell it how executing this stored procedure behind the scenes will magically help with fetching objects from and saving objects to the database.
You can:
IDbConnection
or getting the ISession
's connection, creating an IDbCommand
, etc. Do this if you need a one-off approach to executing stored procedures.Configuration
, to execute this stored procedure when certain other events are sent through the NHibernate pipeline. Only do this if this stored procedure should actually be executed every time and only when these events occur.Do following solutions:
public void Test(TestEntity TestEntity)
{
IQuery query = NHSession.CreateSQLQuery("exec LogData :Time, :Data");
query.SetParameter("Time", TestEntity.Time);
query.SetParameter("Data", TestEntity.Data);
object obj = query.UniqueResult();
}
A Stored Procedure must return a result set. The first parameter of a procedure must be an OUT that returns a result set. This is done by using a SYS_REFCURSOR type in Oracle 9i or later.Even if you do not want to return any result set you must declare first parameter in stored procedure as CURSOR_NAME OUT SYS_REFCURSOR.
In general, calling a procedure that does some other chores and return a result set at the end is not different than making a SELECT
query. Therefore, in the answers above, when executing the query in the last step you need to call
query.List<T>();
where T
is a POCO object that is defined in your code.
This seems to be a limitation of NHibernate, from NHibernate Documentation:
The procedure must return a result set. NHibernate will use IDbCommand.ExecuteReader() to obtain the results.
ExecuteUpdate on SQL Query should help you.
Sample:
ISession session = ....
IQuery query = session.CreateSQLQuery("exec LogData @Time=:time, @Data=:data");
query.SetDateTime("time", time);
query.SetString("data", data);
query.ExecuteUpdate();