views:

38

answers:

1

UPDATED: This is totally wrong assumption. I retested it and sure enough I was mistaken. NHibernate generates SQL that will get all children rows to both Children Lists. Thank sirrocco for the comment. I think the better question is how we could do something like this work.

I modified the code a bit from Fluent NHibernate Examples in Wiki.

Model

public class Parent
{
  public IList<Child> Children1 { get; set; }
  public IList<Child> Children2 { get; set; }
}

public class Child
{}

Schema

table Parent (
  ID int primary key
)

table Child (
  ID int primary key,
  ParentID int
)

Fluent Mapping

public class ParentMap : ClassMap<Parent>
{
  public ParentMap()
  {
    HasMany<Child>(x => x.Children1); 
    HasMany<Child>(x => x.Children2);
  }
}

As you can see, although the class Parent has two Children lists (i.e., Children1 and Children2), when they are mapped to the database tables, there is nothing in the table saying that which children row should be in Children1 or Children2 lists.

From database perspective, we know only which children row belongs to which parent but not which Children lists.

However, this seems to work correctly in NHibernate. Is there anything happening behind the scene?

+1  A: 

Are you sure it works correctly ? I just tested this and added one child to each collection and as expected each collection had the same 2 children.

sirrocco
Yes, you are correct. It is not really working. I think I was mistaken because I test it in one NHibernate session and NHibernate did not create object from database.
kimsk