NHibernate: populate list with uncommitted data related only by FK in database

174 views Asked by At

This is an elaboration and clarification of this question.

Suppose I have two tables, Foo and Bar.

Bar has a FK to Foo.

In the application, the tables are represented by classes and Foo has a list of Bars. Bar has a property for the id of the Foo it has a FK to in the database.

In the context of a Session and Transaction with IsolationLevel.ReadUncommitted, I add an instance of Foo to the database, assign the generated id to the Foo_id property of an instance of Bar and also add it to the database.

Now, before calling Transaction.Commit(), is it possible to have NHibernate read out Foo with a list of Bar from the database? That is, read the data uncommitted?

I have created a VS2012 project which demonstrates this. It includes a SSDT project for building the required database and has tests showing what I asking about.

Thank you.

1

There are 1 answers

5
Oskar Berggren On BEST ANSWER

In the context of the same transaction you can read back all changes you've made to the database, so yes. The isolation level of the transaction doesn't matter for changes made within that transaction.

However, NHibernate typically won't update an already loaded object. But if you Flush(), followed by either Clear() or Evict() and then read Foo again, its collection will include the instance of Bar. (As long as you are in the same session, committing the transaction is irrelevant to this, except that with default settings Commit() automatically calls Flush().)