NHibernate - 使用集合缓存时重复结果

时间:2013-03-27 20:07:24

标签: nhibernate nhibernate-caches

当使用具有多层双向亲子(-grandchild)一对多关系的二级缓存时,我从NHibernate获得了非常奇怪的行为:

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
}

第二个孩子,有两个孙子,在集合中重复,但具有相同的对象ID(ReferenceEquals()true)。当我禁用Parent.Children集合的缓存,或将获取策略设置为Join时,问题就会消失。任何想法如何找出这里发生的事情?

实体:

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;
    }

}

映射:

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();
    }
}

[编辑]

我通过不为子集合启用缓存来解决此问题。由于我是首先为了避免N + 1选择问题而对缓存实体的子项进行fetch join查询,所以我做了以下事情:

    public ChildMap()
    {
       // ...
        HasMany(c => c.GrandChildren) // removed Cache.ReadWrite() here
            /* ... */;

        // added IncludeAll() to make sure lazily fetched collections get cached
        Cache.ReadWrite().IncludeAll(); 
    }

我仍然不明白这里发生了什么,所以对此事的任何见解仍然会受到欢迎: - )。

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用交易代替海运冲洗?像

using(var session = sessionFactory.OpenSession())
using(var transaction = session.BeginTransaction())
{
    var parent = new Parent();
    [...]
    parent.Save();
    transaction.Commit();
    id = parent.Id;
}

也许这有帮助。我曾经遇到类似的问题,我可以使用交易来解决。