LMC
LMC

Reputation: 103

Nhibernate order of SQL statements when flushing session

According to NHibernate documentation, SQL statements are issued in the following order when a session is flushed:

Why is it forced in this order and is there any way to change it so that the statements are executed in the same order that I give them?

Upvotes: 8

Views: 2751

Answers (2)

Andrew dh
Andrew dh

Reputation: 879

You can't change the order NHibernate generates SQL in per the above, but you can chunk up the units of work.

eg:

using(var transaction = Session.BeginTransaction())
{
    var company = Session.QueryOver<Company>().First();
    var employee = new Employee{ ID = Guid.NewID() };
    company.Employees.Add(employee);
    Session.Flush();    

    var carSpace = new CarParkingSpace { EmployeeID = employee.ID };
    Session.Save(carSpace);

    transaction.Commit();
}

By adding the Session.Flush() - everything to that point will be pushed to the transaction. Without it, NHibernate would try to create a parking space allocated to a not yet existing employee.

Upvotes: 1

Diego Mijelshon
Diego Mijelshon

Reputation: 52753

It's in that order because it's the safest.

And no, you can't change the order. But also, you never gave NHibernate any order: you just mark entities for persistence; NHibernate determines what to do automatically.

If you think you need more control over individual SQL operations, you can use IStatelessSession instead of a regular ISession. You lose all the stuff NH does automatically (lazy loading, caching, dirty-tracking), but you can (must) say explicitly when to Insert, Delete or Update a record.

Upvotes: 4

Related Questions