NHibernate多对多 [英] NHibernate Many-to-many
问题描述
我有一个旧数据库,我正在尝试创建NHibernate DAL. 我在多对多"表上进行映射时遇到问题.
I have a legacy database and I am trying to create a NHibernate DAL. I have a problem with a mapping on Many-To-Many table.
数据库表:
-
studio_Subscribers
-
studio_Groups
(包含一个订户的IList) -
studio_Subscribers_Groups
-具有主键的多对多表
studio_Subscribers
studio_Groups
(contains a IList of Subscribers)studio_Subscribers_Groups
- Many-To-Many table with primary keys
问题是,当我创建一个SubscriberGroup
实例并用订户填充它时,它们将被保存到studio_Subscribers
表中,而不是保存到多对多"表中.
The problem is when I create a SubscriberGroup
instance and fill it with Subscribers they gets saved to the studio_Subscribers
table but not to the Many-To-Many table.
我不知道怎么了?
studio_Subscribers
表映射:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Meridix.Studio.Common"
namespace="Meridix.Studio.Common">
<class name="SubscriberItem" table="studio_Subscribers">
<id name="StorageId" column="Id" unsaved-value="0" access="nosetter.camelcase">
<generator class="identity" />
</id>
<property name="Id" column="DomainId" not-null="true" />
<property name="Subscriber" column="Subscriber" not-null="true" length="50" />
<property name="Description" column="Description" not-null="false" length="100" />
<property name="Type" column="Type" not-null="true" length="40"
type="Meridix.Studio.Data.Repositories.EnumStringTypes.SubscriberTypeEst, Meridix.Studio.Data.Repositories" />
</class>
</hibernate-mapping>
studio_Groups
表映射:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Meridix.Studio.Common"
namespace="Meridix.Studio.Common">
<class name="SubscriberGroup" table="studio_Groups">
<id name="StorageId" column="Id" unsaved-value="0" access="nosetter.camelcase">
<generator class="identity" />
</id>
<property name="Id" column="DomainId" not-null="true" />
<property name="Name" column="Name" not-null="true" length="200" />
<property name="Description" column="Description" not-null="false" length="300" />
<bag name="Subscribers" table="studio_Groups_Subscribers" access="nosetter.camelcase">
<key column="GroupId"></key>
<many-to-many column="SubscriberId" class="SubscriberItem" />
</bag>
</class>
</hibernate-mapping>
推荐答案
您是否应该在订户上拥有与该组多对多关系的合适包包?
Shouldn't you have an appropriate bag on your subscriber aswell with a many-to-many relation to the group?
<bag name="Groups" table="studio_Groups_Subscribers" access="nosetter.camelcase">
<key column="SubscriberId"></key>
<many-to-many column="GroupId" class="GroupItem" />
</bag>
,并且在您的SubscriberItems集合上可能有一个cascade ="save-update",以便保存组将通过另一端"的适当关系来更新您的孩子.
and maybe have a cascade="save-update" on your SubscriberItems collection so that saving the Group will update your children with the appropriate relation from "the other side".
这篇关于NHibernate多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!