Reputation: 2621
I am getting very strange behavior from NHibernate when using the second level cache with multiple layers of bi-directonal parent-child(-grandchild) one-to-many relationships:
int id;
using(var session = sessionFactory.OpenSession())
{
var parent = new Parent();
var child1 = parent.AddChild();
child1.AddGrandChild();
var child2 = parent.AddChild();
child2.AddGrandChild();
session.Save(parent);
session.Flush(); // force id generation
id = parent.Id;
}
using(var session = sessionFactory.OpenSession())
{
var parent = session.Get<Parent>(id);
parent.Children.Should().HaveCount(2); // but is actually 3; second child duplicated
}
The second child, which has two grandchildren, gets duplicated in the collection, but with the same object id (ReferenceEquals()
is true
). When I disable caching for the Parent.Children
collection, or set the fetching strategy to Join
, the problem goes away. Any ideas how to find out what is happening here?
Entities:
class Parent
{
public virtual int Id { get; protected set; }
public virtual List<Child> children = new List<Child>();
public virtual IEnumerable<Child> Children
{
get { return children.ToList(); }
}
public virtual Child AddChild()
{
var child = new Child(this);
this.children.Add(child);
return child;
}
}
class Child
{
public Child(Parent parent)
{
this.Parent = parent;
}
public virtual int Id { get; protected set; }
public virtual List<GrandChild> grandChildren = new List<GrandChild>();
public virtual IEnumerable<GrandChild> GrandChildren
{
get { return grandChildren.ToList(); }
}
public virtual Parent Parent { get; protected set; }
public virtual GrandChild AddGrandChild()
{
var grandChild = new GrandChild(this);
this.grandChildren.Add(grandChild);
return grandChild;
}
}
class GrandChild
{
public virtual int Id { get; protected set; }
public virtual Child Parent { get; protected set; }
public GrandChild(Child parent)
{
this.Parent = parent;
}
}
Mappings:
public class ParentMap : ClassMap<Parent>
{
public ParentMap()
{
Id(p => p.Id).GeneratedBy.Identity();
HasMany(p => p.Children)
.Access.CamelCaseField()
.Inverse()
.Cascade.AllDeleteOrphan()
.Cache.ReadWrite();
Cache.ReadWrite();
}
}
public class ChildMap : ClassMap<Child>
{
public ChildMap()
{
Id(c => c.Id).GeneratedBy.Identity();
HasMany(c => c.GrandChildren)
.Access.CamelCaseField()
.Inverse()
.Cascade.AllDeleteOrphan()
.Cache.ReadWrite();
References(c => c.Parent);
Cache.ReadWrite();
}
}
public class GrandChildMap : ClassMap<GrandChild>
{
public GrandChildMap()
{
Id(c => c.Id).GeneratedBy.Identity();
References(c => c.Parent);
Cache.ReadWrite();
}
}
[EDIT]
I worked around this issue by not enabling the cache for the child collections. Since I did it in the first place in order to avoid N+1 select issues with fetch join
queries on children of cached entities, I did the following instead:
public ChildMap()
{
// ...
HasMany(c => c.GrandChildren) // removed Cache.ReadWrite() here
/* ... */;
// added IncludeAll() to make sure lazily fetched collections get cached
Cache.ReadWrite().IncludeAll();
}
I still don't understand what's going on here though, so any insight into this matter would still be welcome :-).
Upvotes: 1
Views: 226
Reputation: 732
Have you tried using transactions instead of seassion flushing? Like
using(var session = sessionFactory.OpenSession())
using(var transaction = session.BeginTransaction())
{
var parent = new Parent();
[...]
parent.Save();
transaction.Commit();
id = parent.Id;
}
Maybe that helps. I once had a similar problem that I could solve with using transactions.
Upvotes: 0