Hi i am using the transaction-per-request (session-in-view) pattern for an asp.net web application. I have a couple of points in the application where i want to Save an NHiberna
Have you tried using NHibernate's direct SQL features? Instead of breaking out into SQLCommand which NHibernate knows nothing about you can use ISession.CreateSQLQuery("native SQL here"). If you then do this inside a transaction after your Save method NHibernate should execute all the statements in order when the Commit happens. Thus:
using(var tx = session.BeginTransaction())
{
try
{
FooClass fc = new FooClass("value");
nhsession.Save(fc);
var nativeQuery = nhsession.CreateSQLQuery("some insert/update query that depends on fc's id");
// Add parameters to nativeQuery using SetXXX methods
nativeQuery.SetXXX(...);
// Save nativeQuery
nativeQuery.Save(); // I think...
tx.Commit();
}
catch (...)
{
tx.Rollback();
}
}
See this for another similar question regarding using the NH native query: Hibernate NHibernate - Native SQL