tags:

views:

64

answers:

2

Is my understanding correct, that the following class and table design is not possible in NHibernate:

public class Parent
{
   public virtual Guid Id { get; set; }
   public virtual ISet<Child> Children { get; set; }
   ...
}

public class Child
{
   public virtual Guid Id { get; set; }
   ...
}

table Parent { Id, ... }
table Child { Id, ParentId (not null), ... }

So notice the following:

  • a one-to-many association between parent and child
  • using an ISet
  • no bidirectional association from Child back to Parent
  • Child's ParentId column is not null
A: 

The situation you describe is very much intended by and supported by NHibernate.

Justice
How can I do this - according to the docs:"If the <key> column of a <one-to-many> association is declared NOT NULL, NHibernate may cause constraint violations when it creates or updates the association. To prevent this problem, you must use a bidirectional association with the many valued end (the set or bag) marked as inverse="true"."
cbp
So try using `<set inverse="true" />` in the mapping for the parent's collection-of-children.
Justice
But its one of my requirements that I don't want a bi-directional association, because it makes the model more confusing.
cbp
+1  A: 

My knee jerk reaction was to say that that it was supported but I think you are right. Your options are either - bidirectional relationship or nullable ParentId column in Child table.

Here's a interesting thread where Ayende gets into it in the bug tracker for NH http://nhjira.koah.net/browse/NH-1050 (it's the previous release of NH but I think the answer is the same)

Michael Gattuso