我有AssetGroup實體與資產實體具有一對多關係。有一個實體基類覆蓋了Equals和GetHashCode。我下面的ch 20 parent child與NHibernate一對多關係問題級聯收集
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="TestNHibernate"
namespace="TestNHibernate.Models" auto-import="true">
<class name="AssetGroup">
<id name="Id" column="Id" type="guid">
<generator class="guid"></generator>
</id>
<property name="Name" type="string" not-null="true"/>
<set name="Assets" cascade="all" inverse="true" access="field.camelcase-underscore" lazy="true">
<key column="AssetGroupID"/>
<one-to-many class="Asset"/>
</set>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="TestNHibernate"
namespace="TestNHibernate.Models" auto-import="true">
<class name="Asset">
<id name="Id" column="Id" type="guid">
<generator class="guid"></generator>
</id>
<property name="Name" type="string" not-null="true"/>
<many-to-one name="AssetGroup" column="AssetGroupID" cascade="all" lazy="false"/>
</class>
</hibernate-mapping>
代碼爲folloow的例子:
public class AssetGroup : Entity<Guid>
{
public AssetGroup()
{
this._assets = new HashedSet<Asset>();
}
virtual public string Name { get; set; }
private ISet<Asset> _assets;
virtual public ISet<Asset> Assets
{
get { return _assets; }
protected set { _assets = value; }
}
virtual public bool AddAsset(Asset asset)
{
if (asset != null && _assets.Add(asset))
{
asset.SetAssetGroup(this);
return true;
}
return false;
}
virtual public bool RemoveAsset(Asset asset)
{
if (asset != null && _assets.Remove(asset))
{
asset.SetAssetGroup(null);
return true;
}
return false;
}
}
public class AssetGroup : Entity<Guid>
{
public AssetGroup()
{
this._assets = new HashedSet<Asset>();
}
virtual public string Name { get; set; }
private ISet<Asset> _assets;
virtual public ISet<Asset> Assets
{
get { return _assets; }
protected set { _assets = value; }
}
virtual public bool AddAsset(Asset asset)
{
if (asset != null && _assets.Add(asset))
{
asset.SetAssetGroup(this);
return true;
}
return false;
}
virtual public bool RemoveAsset(Asset asset)
{
if (asset != null && _assets.Remove(asset))
{
asset.SetAssetGroup(null);
return true;
}
return false;
}
}
我TestCode情況如下:
[TestMethod]
public void Can_Use_ISession()
{
ISession session = TestConfig.SessionFactory.GetCurrentSession();
var ag = new AssetGroup { Name = "NHSession" };
session.Save(ag);
var a1 = new Asset { Name = "s1" };
var a2 = new Asset { Name = "s2" };
a1.SetAssetGroup(ag);
a2.SetAssetGroup(ag);
session.Flush();
Assert.IsTrue(a1.Id != default(Guid)); // ok
Assert.IsTrue(a2.Id != default(Guid)); // ok
var enumerator = ag.Assets.GetEnumerator();
enumerator.MoveNext();
Assert.IsTrue(ag.Assets.Contains(enumerator.Current)); // failed
Assert.IsTrue(ag.Assets.Contains(a1)); // failed
Assert.IsTrue(ag.Assets.Contains(a2)); // failed
var agRepo2 = new NHibernateRepository<AssetGroup>(TestConfig.SessionFactory, new QueryFactory(TestConfig.Locator));
Assert.IsTrue(agRepo2.Contains(ag)); // ok
var ag2 = agRepo2.FirstOrDefault(x => x.Id == ag.Id);
Assert.IsTrue(ag2.Assets.FirstOrDefault(x => x.Id == a1.Id) != null); // ok
Assert.IsTrue(ag2.Assets.FirstOrDefault(x => x.Id == a2.Id) != null); // ok
var aa1 = session.Get<Asset>(a1.Id);
var aa2 = session.Get<Asset>(a2.Id);
Assert.IsTrue(ag2.Assets.Contains(aa1)); // failed
Assert.IsTrue(ag2.Assets.Contains(aa2)); // failed
}
我的實體基類是在這裏:
public abstract class Entity<Tid> : IEquatable<Entity<Tid>>
{
[HiddenInput(DisplayValue = false)]
public virtual Tid Id { get; protected set; }
public override bool Equals(object obj)
{
if (obj == null)
return base.Equals(obj);
return Equals(obj as Entity<Tid>);
}
public static bool IsTransient(Entity<Tid> obj)
{
return obj != null && Equals(obj.Id, default(Tid));
}
private Type GetUnproxiedType()
{
return GetType();
}
public virtual bool Equals(Entity<Tid> other)
{
if (ReferenceEquals(this, other))
return true;
if (!IsTransient(this) && !IsTransient(other) && Equals(Id, other.Id))
{
var otherType = other.GetUnproxiedType();
var thisType = GetUnproxiedType();
return thisType.IsAssignableFrom(otherType) || otherType.IsAssignableFrom(thisType);
}
return false;
}
public override int GetHashCode()
{
if (Equals(Id, default(Tid)))
{
return base.GetHashCode();
}
else
{
return Id.GetHashCode();
}
}
}
我哈哈已經評論哪些部分在代碼中失敗了。請幫忙。看來通過級聯保存的實體與ICollection Contains/Remove不兼容。資產a1 a2已保存,它們位於父級集合中。例如,我可以通過Linq FirstOrDefault找到它們。但集合的包含和刪除將無法找到它們。我注意到Collection使用GetHashCode而Contains()或Remove()被調用。
我正在使用Iesi HashSet –