如何在 NHibernate 中映射多对多对多的三元关系?
How to map a many-to-many-to-many ternary relationship in NHibernate?
正在尝试建立多对多对多关联。
我目前的情况是这样的:
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; }
}
}
这是我的映射:
<?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.PersistentClass, com.example" />
</map>
</class>
<class name="com.example.PersistentClass, com.example">
<id name="Id" type="Int64" generator="hilo" />
<property name="Prop" />
</class>
</hibernate-mapping>
创建模式生成以下内容SQL(SqlServer 示例):
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FKC8D94E45A4783B9]') AND parent_object_id = OBJECT_ID('MappedCollections'))
alter table MappedCollections drop constraint FKC8D94E45A4783B9
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FKC8D94E46534DBE0]') AND parent_object_id = OBJECT_ID('MappedCollections'))
alter table MappedCollections drop constraint FKC8D94E46534DBE0
if exists (select * from dbo.sysobjects where id = object_id(N'Foo') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table Foo
if exists (select * from dbo.sysobjects where id = object_id(N'MappedCollections') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table MappedCollections
if exists (select * from dbo.sysobjects where id = object_id(N'PersistentClass') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table PersistentClass
if exists (select * from dbo.sysobjects where id = object_id(N'hibernate_unique_key') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table hibernate_unique_key
create table Foo (
Id BIGINT not null,
primary key (Id)
)
create table MappedCollections (
Id BIGINT not null,
elt BIGINT not null,
Key NVARCHAR(255) not null,
primary key (Id, Key) -- !! WRONG !! should be unique (Id, elt, Key)
)
create table PersistentClass (
Id BIGINT not null,
Prop NVARCHAR(255) null,
primary key (Id)
)
alter table MappedCollections
add constraint FKC8D94E45A4783B9
foreign key (elt)
references PersistentClass
alter table MappedCollections
add constraint FKC8D94E46534DBE0
foreign key (Id)
references Foo
create table hibernate_unique_key (
next_hi BIGINT
)
insert into hibernate_unique_key values ( 1 )
知道我做错了什么吗?从我们的 SQL 中,我们可以看到它一直保持为 IDictionary<string, PersistentClass>
而不是 IDictionary<string, ISet<PersistentClass>
,我不想要许多 Foo
对多对的多对多关系string
和 Persistent
class,其中每对 Foo
都是唯一的。所有 three 值都应创建一个唯一值。
我该怎么做?
(注意:我包含了 Hibernate 标签,因为无论是 Hibernate 还是 NHibernate,这种关系的 xml 映射应该是相同的)
假设您有一个名为 Product { productid,name) 的 table 和一个名为 Bundle {bundleid, name} 的 table。现在 Product 和 bundle 可以有多对多的映射。因此,创建一个名为 BundleProduct {BundleId, ProductId} 的新 table。现在 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();
}
虽然它创建了一个不必要的连接,但创建另一个实体可以做到这一点,同时保持非常相似的 public 界面。
基本上:
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>
正在尝试建立多对多对多关联。
我目前的情况是这样的:
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; }
}
}
这是我的映射:
<?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.PersistentClass, com.example" />
</map>
</class>
<class name="com.example.PersistentClass, com.example">
<id name="Id" type="Int64" generator="hilo" />
<property name="Prop" />
</class>
</hibernate-mapping>
创建模式生成以下内容SQL(SqlServer 示例):
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FKC8D94E45A4783B9]') AND parent_object_id = OBJECT_ID('MappedCollections'))
alter table MappedCollections drop constraint FKC8D94E45A4783B9
if exists (select 1 from sys.objects where object_id = OBJECT_ID(N'[FKC8D94E46534DBE0]') AND parent_object_id = OBJECT_ID('MappedCollections'))
alter table MappedCollections drop constraint FKC8D94E46534DBE0
if exists (select * from dbo.sysobjects where id = object_id(N'Foo') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table Foo
if exists (select * from dbo.sysobjects where id = object_id(N'MappedCollections') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table MappedCollections
if exists (select * from dbo.sysobjects where id = object_id(N'PersistentClass') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table PersistentClass
if exists (select * from dbo.sysobjects where id = object_id(N'hibernate_unique_key') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table hibernate_unique_key
create table Foo (
Id BIGINT not null,
primary key (Id)
)
create table MappedCollections (
Id BIGINT not null,
elt BIGINT not null,
Key NVARCHAR(255) not null,
primary key (Id, Key) -- !! WRONG !! should be unique (Id, elt, Key)
)
create table PersistentClass (
Id BIGINT not null,
Prop NVARCHAR(255) null,
primary key (Id)
)
alter table MappedCollections
add constraint FKC8D94E45A4783B9
foreign key (elt)
references PersistentClass
alter table MappedCollections
add constraint FKC8D94E46534DBE0
foreign key (Id)
references Foo
create table hibernate_unique_key (
next_hi BIGINT
)
insert into hibernate_unique_key values ( 1 )
知道我做错了什么吗?从我们的 SQL 中,我们可以看到它一直保持为 IDictionary<string, PersistentClass>
而不是 IDictionary<string, ISet<PersistentClass>
,我不想要许多 Foo
对多对的多对多关系string
和 Persistent
class,其中每对 Foo
都是唯一的。所有 three 值都应创建一个唯一值。
我该怎么做?
(注意:我包含了 Hibernate 标签,因为无论是 Hibernate 还是 NHibernate,这种关系的 xml 映射应该是相同的)
假设您有一个名为 Product { productid,name) 的 table 和一个名为 Bundle {bundleid, name} 的 table。现在 Product 和 bundle 可以有多对多的映射。因此,创建一个名为 BundleProduct {BundleId, ProductId} 的新 table。现在 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();
}
虽然它创建了一个不必要的连接,但创建另一个实体可以做到这一点,同时保持非常相似的 public 界面。
基本上:
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>