nhibernatehas-manyunit-of-workread-uncommitted

Read uncomitted data from HasMany-relationship with NHibernate


I have an NHibernate map which defines a HasMany-relationship on a type, i.e. a class has a list of another class.

I would like NHibernate to be able to read uncommitted data including the list resulting from the HasMany-relationship.

I have isolationlevel ReadUncomitted and I am able to write data and read it back before committing.

However, the list is always empty, unless I commit first.

Is there a way to make NHibernate populate objects with data from HasMany-relationships?

EDIT

It turns out any non-primitive types on a class are not populated by an uncommitted read unless they are actually added to the class.

For example, class Foo below has a list of Member which - in the database - are connected by Id. If I persist an instance of Foo and an instance of Member to the database using NHibernate and both are in fact related by the value of Id, then Foo will not have the expected instance of Member if I read it back uncommitted (i.e. before completing the transaction).

public class Member
{
     Guid Id{ get; set; }
}

public class Foo
{
    List<Member> MemberList{ get; set; }
}

// the mapping
public class FooMap
{
    HasMany(x => x.MemberList).KeyColumn("Id").PropertyRef("Id");
}

However, If I create an instance of Foo and an instance of Member and set the latter as a reference of the former and persist both to the database using NHibermate, then Foo will have the expected instance of Member when I read it back before completing the transaction.

If I complete the transaction then Foo will have the expected instance of Member on subsequent reads; as long as the transaction is completed it is irrelevant whether Member existed only as a database record with the correct FK to Foo or it was a reference to Foo.

Revised Question: It is possible have NHibernate populate complex members based on FK-relationships only during an uncommitted read?


Solution

  • There seems to be some general confusion here, so to clarify a few things: