I have written an integrationtest that fails:
[Test]
public void Find_WorkItemWithMatchingDescriptionExistsInRepository_ReturnsWorkItem()
{
// arrange
WorkItemRepository repository = new WorkItemRepository(IsolatingFactory);
const string Description = "A";
WorkItem itemWithMatchingDescription = WorkItem.Create(1, Description);
repository.Commit(itemWithMatchingDescription);
// act
List<WorkItem> searchResult = repository.Find(Description);
// assert
CollectionAssert.Contains(searchResult, itemWithMatchingDescription);
}
When watching the sql output from nhibernate i notice that it creates a select-statement for the repository.Find() operation, but no insert operation before the select. If i put an explicit session.Flush() after the repository.Commit it works as expected, but I find this behaviour odd. Why doesn't nhibernate flush the session automatically when there are pending insert and a query is performed against the session?
I found this in the documentation:
9.6. Flush
From time to time the ISession will execute the SQL statements needed to synchronize the ADO.NET connection's state with the state of objects held in memory. This process, flush, occurs by default at the following points
* from some invocations of Find() or Enumerable() * from NHibernate.ITransaction.Commit() * from ISession.Flush()
It looks as the default nhibernate behaviour is to flush before quering by find. My repository uses Ling to nhibernate for the query specification, maybe this is a bug.
Update
If I change my mapping from:
public WorkItemClassMap()
{
Not.LazyLoad();
Id(wi => wi.Id).GeneratedBy.Assigned();
Map(wi => wi.Description).Length(500);
Version(wi => wi.LastChanged).UnsavedValue(new DateTime().ToString());
}
To:
public WorkItemClassMap()
{
Not.LazyLoad();
Id(wi => wi.Id).GeneratedBy.Identity();
Map(wi => wi.Description).Length(500);
}
It works as expected (nhibernate flushes the insert to the database before performing the query). I can't see any good reason for this behavior so I'm assuming its a bug. Looks like a need to do manual flush before querying in my repositories, which is not something I really want to do.