Force query execution without flush/commit

后端 未结 5 1107
太阳男子
太阳男子 2021-02-09 15:59

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

5条回答
  •  难免孤独
    2021-02-09 16:37

    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

提交回复
热议问题