1

我只是无法理解它抛出 NonUniqueObjectException 的原因。NHibernate 文档明确指出:

saveOrUpdate() does the following:
if the object is already persistent in this session, do nothing
if another object associated with the session has the same identifier, throw an exception
....

因此,仅当与会话关联的另一个对象具有相同的标识符时,它才应该抛出异常。我的对象覆盖了它们的 Equals 和 GetHashCode(_idCopy 用于另一种情况,对于我的情况始终为空):

    public override bool Equals(object obj)
    {
        return Equals(obj as SimplePersistantEqualSupported);
    }

    public override int GetHashCode()
    {
        if (_idCopy != null)
        {
            return _idCopy.GetHashCode();
        }

        return Id.GetHashCode();
    }

    public virtual bool Equals(SimplePersistantEqualSupported other)
    {
        if (other == null)
        {
            return false;
        }

        if (ReferenceEquals(this, other))
        {
            return true;
        }

        if (_idCopy != null)
        {
            // User _idCopy instead of id.
            if (!IsTransientIdCopy(this) && !IsTransientIdCopy(other) && Equals(_idCopy, other._idCopy))
            {
                return GetType().IsAssignableFrom(other.GetType()) ||
                       other.GetType().IsAssignableFrom(GetType());
            }

            return false;
        }

        if (!IsTransient(this) && !IsTransient(other) && Equals(Id, other.Id))
        {
            return GetType().IsAssignableFrom(other.GetType()) ||
                   other.GetType().IsAssignableFrom(GetType());
        }

        return false;
    }

    private static bool IsTransient(SimplePersistantEqualSupported obj)
    {
        return obj != null && Equals(obj.Id, default(int));
    }

    private static bool IsTransientIdCopy(SimplePersistantEqualSupported obj)
    {
        return obj != null && Equals(obj._idCopy, default(int));
    }

我知道我在会话中有具有相同标识符的对象。我使用分离的对象,这就是它的本意。但由于它们是平等的——它们不是另一个对象,它们是相同的。当我查看 NHibernate 源时,我没有看到对 .Equals() 对象的检查 - 只要会话中存在具有相同标识符的任何内容,它就会抛出异常:

public void CheckUniqueness(EntityKey key, object obj)
    {
        object entity = GetEntity(key);
        if (entity == obj)
        {
            throw new AssertionFailure("object already associated, but no entry was found");
        }
        if (entity != null)
        {
            throw new NonUniqueObjectException(key.Identifier, key.EntityName);
        }
    }

当我此时调试时,我可以确保obj.Equals(GetEntity(key)) == trueGetEntity(key).Equals(obj) == true. 我究竟做错了什么?

我不想在应用程序中使用 .Merge,因为任何对象都可以在会话中克隆它,所以这意味着我必须将每个 SaveOrUpdate 更改为 Merge。

更新。我试图 SaveOrUpdate 的对象有另一个对象作为双向多对多。所以 NHibernate 遍历对象树,去my object-> another object that many-to-many-> my object,然后失败了,即使 firstmy object和 secondmy object通过引用是相等的!

4

1 回答 1

1

NHibernate 不必在那里做同样的检查。它已经知道给定实体不在会话中,因为StatefulPersistenceContext.GetEntry()返回 null。它确实返回 null 因为它通过引用相等搜索条目。因此,如果会话中的任何其他对象具有与给定对象相同的 Id,则它必须引发异常以遵循文档。

两个相等的对象不必相同。“相同”通常用来表示两个引用指向同一个对象(引用相等)。

PS:与您的问题无关,但您的实现似乎Equals对于比较使用继承映射的实体的代理并不安全(代理仅继承基类,因此不能分配给继承的类)。

于 2013-06-25T09:08:41.423 回答