我只是无法理解它抛出 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)) == true
和GetEntity(key).Equals(obj) == true
. 我究竟做错了什么?
我不想在应用程序中使用 .Merge,因为任何对象都可以在会话中克隆它,所以这意味着我必须将每个 SaveOrUpdate 更改为 Merge。
更新。我试图 SaveOrUpdate 的对象有另一个对象作为双向多对多。所以 NHibernate 遍历对象树,去my object
-> another object that many-to-many
-> my object
,然后失败了,即使 firstmy object
和 secondmy object
通过引用是相等的!