首先,这段代码可能存在问题,我最初没有写过。
我有3个类:ClassA,ClassB和InheritedClassA。 ClassA和InheritedClassA是在两个数据库之一上运行的公共代码的一部分,这两个数据库只有一个稍微不同的TableA结构,InheritedClassA映射到的表有一些额外的列,但在其他方面与ClassA映射到的表相同。
以下是POCO和映射:
public class ClassA
{
private IList<ClassB> _classBsBag;
private Collection<ClassB> _classBs;
public ClassA() { }
public virtual Guid ClassAId { get; set; }
public virtual int Version { get; set; }
public virtual Collection<ClassB> ClassBs
{
get
{
return this._classBs;
}
set
{
this._classBsBag = new List<ClassB>();
if (value != null)
{
foreach (ClassB classB in value)
{
this._classBsBag.Add(classB);
}
}
this._classBs = value;
}
}
public virtual IList<ClassB> ClassBsBag
{
get
{
return this._classBsBag;
}
set
{
this._classBs = (value == null) ? null : new Collection<ClassB>(value);
this._classBsBag = value;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping auto-import="false" xmlns="urn:nhibernate-mapping-2.2">
<class name="MyNamespace.ClassA, MyNamespace" lazy="false" dynamic-update="true" dynamic-insert="true" table="TableA" polymorphism="explicit">
<id name="ClassAId" column="ClassAId">
<generator class="MyNamespace.GuidGenerator, MyNamespace" />
</id>
<version name="Version" unsaved-value="0" />
<bag name="ClassBsBag" lazy="false" cascade="all-delete-orphan" inverse="true">
<key column="ClassAId" />
<one-to-many class="MyNamespace.ClassB, MyNamespace" not-found="ignore" />
</bag>
</hibernate-mapping>
public class ClassB
{
public ClassB() { }
public virtual Guid ClassBId { get; set; }
public virtual int Version { get; set; }
public virtual ClassA ClassA { get; set; }
}
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping auto-import="false" xmlns="urn:nhibernate-mapping-2.2">
<class name="MyNamespace.ClassB, MyNamespace" lazy="false" table="TableB" polymorphism="explicit">
<id name="ClassBId" type="Guid">
<generator class="MyNamespace.GuidGenerator, MyNamespace" />
</id>
<version name="Version" unsaved-value="0" />
<many-to-one name="ClassA" class="MyNamespace.ClassA, MyNamespace" column="ClassAId" lazy="false" />
</class>
</hibernate-mapping>
public class InheritedClassA : ClassA
{
public InheritedClassA() { }
public virtual string AdditionalField { get; set; }
}
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping auto-import="false" xmlns="urn:nhibernate-mapping-2.2">
<class name="MyNamespace.InheritedClassA, MyNamespace" lazy="false" dynamic-update="true" dynamic-insert="true" table="TableA" polymorphism="explicit">
<id name="ClassAId" column="ClassAId">
<generator class="MyNamespace.GuidGenerator, MyNamespace" />
</id>
<version name="Version" unsaved-value="0" />
<property name="AdditionalField" column="AdditionalField" length="4" not-null="true" />
<bag name="ClassBsBag" lazy="false" cascade="all-delete-orphan" inverse="true">
<key column="ClassAId" />
<one-to-many class="MyNamespace.ClassB, MyNamespace" not-found="ignore" />
</bag>
</hibernate-mapping>
ClassA在TableA上适用于两个数据库,插入,更新和删除,包括从列表中删除ClassB。 InheritedClassA与ClassA的工作方式相同,但从列表中删除ClassB除外。查看对象图,InheritedClassA有一个ClassB列表,每个ClassB都有一个包含ClassB列表的ClassA,包括从InheritedClassA列表中删除的任何类。
老实说,这对我来说确实有点可疑,但过去常常有用,现在却没有,我想弄明白为什么。我无法确定提交历史中的任何重大更改,也不知道它何时退出工作(很长的故事)。
代码库有一个怪异的协调例程,用于保存集合和处理分离的数据,我认为问题的一部分可能在那里。当一个InheritedClassA从列表中删除了一个ClassB成员并尝试保存时,我会收到以下内容:
NHibernate.ObjectDeletedException:删除的对象将被重新保存 级联(从关联中删除已删除的对象)
非常感谢任何协助。
答案 0 :(得分:1)
It's difficult to pinpoint the exact problem, but the following could perhaps be a clue.
The collections are mapped with inverse. This tells us that if instanceB1 is included in the collection owned by instanceA1, then instanceB1.ClassA should reference exactly instanceA1, and not some other instance of ClassA.
If instanceA1 and instanceA2 both include instanceB1 in their collections, instanceB1 can still only point to one A instance. Check if the offending instanceB1 is still included in some other collection. If it's resaved and its ClassA property still points to the instance of ClassA that owns the collection from which it was removed, the resave would make it appear in the collection again.