I have two objects that reference each other. From a purely schema perspective, object one could have many instances of object two that reference it, but the business logic specifies that each instance of object 2 will reference a unique instance of object one and vice versa.
Example:
public class Object1 {
public Guid Id {get;set;}
public Object2 Object2 {get;set;}
public Object1ClassMap : ClassMap<Object1>
{
// ...
References<Object2>(x=>x.Object2)
.Column("Object2Id")
.Cascade.SaveUpdate()
.Not.LazyLoad();
}
}
public class Object2 {
public Guid Id {get;set;}
public Object1 Object2 {get;set;}
public Object2ClassMap : ClassMap<Object1>
{
// ...
References<Object1>(x=>x.Object1)
.Column("Object1Id")
.Cascade.SaveUpdate()
.Not.LazyLoad();
}
}
When I do the following:
instanceOfObject1.Object2 = instanceOfObject2
I would expect NHibernate to detect the back reference and automatically do
instanceOfObject2.Object1 = instanceOfObject1
for me, but this doesn't happen. I have to manually update in both directions. Any way to avoid this?