C# 如何映射NHibernate中的多对多对多三元关系?
试图建立多对多对多的关联 到目前为止,我得到的是:C# 如何映射NHibernate中的多对多对多三元关系?,c#,hibernate,nhibernate,nhibernate-mapping,hibernate-mapping,C#,Hibernate,Nhibernate,Nhibernate Mapping,Hibernate Mapping,试图建立多对多对多的关联 到目前为止,我得到的是: namespace com.example // Assembly = com.example { public class Foo { public virtual long Id { get; set; } public virtual IDictionary<string, ISet<PersistentClass>> MappedCollections { get
namespace com.example // Assembly = com.example
{
public class Foo
{
public virtual long Id { get; set; }
public virtual IDictionary<string, ISet<PersistentClass>> MappedCollections { get; set; }
}
public class PersistentClass
{
public virtual long Id { get; protected set; }
public virtual string Prop { get; set; }
}
}
知道我做错了什么吗?从我们的SQL中,我们可以看到它以
IDictionary
的形式持久化,而不是IDictionary假设您有一个名为Product{productid,name)的表和一个名为Bundle{bundleid,name}的表。现在产品和Bundle可以有多对多的映射。所以创建一个名为BundleProduct{bundleid,productid}的新表.现在Nhibernate映射如下所示-
ProductMap
{
HasManyToMany(x => x.Bundles).Table("BundleProduct").ParentKeyColumn("ProductId").ChildKeyColumn("BundleId").Cascade.None().Inverse().Not.LazyLoad();
}
BundleMap
{
HasManyToMany(x => x.Products)
.Table("BundleProduct")
.ParentKeyColumn("BundleId")
.ChildKeyColumn("ProductId")
.Cascade.All();
}
尽管它创建了一个不必要的连接,但创建另一个实体可以做到这一点,同时保持一个非常相似的公共接口
基本上:
namespace com.example // Assembly = com.example
{
public class Foo
{
public virtual long Id { get; set; }
public virtual ReadOnlyDictionary<string, ISet<PersistentClass>> MappedCollections
{
get
{
return new ReadOnlyDictionary<string, ISet<PersistentClass>>(_mc);
}
}
protected virtual IDictionary<string, PersistentClassSet> _mc { get; set; }
public virtual void InitializeCollection(string key)
{
if (!_mk.ContainsKey(key))
_mc[key] = new PersistentClassSet();
}
}
public class PersistentClass
{
public virtual long Id { get; protected set; }
public virtual string Prop { get; set; }
}
internal class PersistentClassSet : ISet<PersisitentClass>
{
public PersistentClassSet()
{
Proxy = new HashSet<PersistentClass>();
}
protected virtual long Id { get; set; }
protected virtual ISet<PersistentClass> Proxy { get; set; }
public bool Add(PersistentClass item)
{
return Proxy.Add(item);
}
// other ISet implementations delegated to Proxy
}
}
namespace com.example//Assembly=com.example
{
公开课Foo
{
公共虚拟长Id{get;set;}
公共虚拟ReadOnlyDictionary MappedCollections
{
得到
{
返回新的ReadOnlyDictionary(_mc);
}
}
受保护的虚拟IDictionary _mc{get;set;}
公共虚拟void InitializeCollection(字符串键)
{
如果(!_mk.ContainsKey(键))
_mc[key]=新的PersistentClassSet();
}
}
公共类PersistentClass
{
公共虚拟长Id{get;protected set;}
公共虚拟字符串属性{get;set;}
}
内部类PersistentClassSet:ISet
{
公共PersistentClassSet()
{
Proxy=newhashset();
}
受保护的虚拟长Id{get;set;}
受保护的虚拟ISet代理{get;set;}
公共bool添加(PersistentClass项)
{
返回代理。添加(项目);
}
//委托给代理的其他ISet实现
}
}
其映射如下所示:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="com.example.Foo, com.example">
<id name="Id" type="Int64" generator="hilo" />
<map name="MappedCollections">
<key column="Id" />
<index column="Key" type="String" />
<many-to-many class="com.example.PersistentClassSet, com.example" />
</map>
</class>
<class name="com.example.PersistentClass, com.example">
<id name="Id" type="Int64" generator="hilo" />
<property name="Prop" />
</class>
<class name="com.example.PersistentClassSet, com.example">
<id name="Id" type="Int64" generator="hilo" />
<set name="Proxy">
<key column="Id"/>
<many-to-many class="com.example.PersistentClass, com.example" />
</set>
</class>
</hibernate-mapping>
namespace com.example // Assembly = com.example
{
public class Foo
{
public virtual long Id { get; set; }
public virtual ReadOnlyDictionary<string, ISet<PersistentClass>> MappedCollections
{
get
{
return new ReadOnlyDictionary<string, ISet<PersistentClass>>(_mc);
}
}
protected virtual IDictionary<string, PersistentClassSet> _mc { get; set; }
public virtual void InitializeCollection(string key)
{
if (!_mk.ContainsKey(key))
_mc[key] = new PersistentClassSet();
}
}
public class PersistentClass
{
public virtual long Id { get; protected set; }
public virtual string Prop { get; set; }
}
internal class PersistentClassSet : ISet<PersisitentClass>
{
public PersistentClassSet()
{
Proxy = new HashSet<PersistentClass>();
}
protected virtual long Id { get; set; }
protected virtual ISet<PersistentClass> Proxy { get; set; }
public bool Add(PersistentClass item)
{
return Proxy.Add(item);
}
// other ISet implementations delegated to Proxy
}
}
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="com.example.Foo, com.example">
<id name="Id" type="Int64" generator="hilo" />
<map name="MappedCollections">
<key column="Id" />
<index column="Key" type="String" />
<many-to-many class="com.example.PersistentClassSet, com.example" />
</map>
</class>
<class name="com.example.PersistentClass, com.example">
<id name="Id" type="Int64" generator="hilo" />
<property name="Prop" />
</class>
<class name="com.example.PersistentClassSet, com.example">
<id name="Id" type="Int64" generator="hilo" />
<set name="Proxy">
<key column="Id"/>
<many-to-many class="com.example.PersistentClass, com.example" />
</set>
</class>
</hibernate-mapping>