2008-11-25 26 views
5

Tôi có một cơ sở dữ liệu kế thừa và tôi đang cố gắng tạo ra một DAL NHibernate. Tôi gặp sự cố với ánh xạ trên bảng Nhiều người.NHibernate Nhiều-nhiều-nhiều

Các bảng cơ sở dữ liệu:

  • studio_Subscribers
  • studio_Groups (chứa một IList của thuê bao)
  • studio_Subscribers_Groups - Nhiều-To-Nhiều bảng với khóa chính

Vấn đề là khi Tôi tạo một phiên bản SubscriberGroup và điền vào nó với Người đăng ký họ được lưu vào bảng studio_Subscribers nhưng không đến bảng Nhiều người.

Tôi không thể tìm ra điều gì sai?

studio_Subscribers bảng vẽ bản đồ:

<?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 mapping bảng:

<?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> 

Trả lời

2

nên không bạn có một túi thích hợp trên là tốt thuê bao của bạn với một mối quan hệ nhiều-nhiều vào nhóm ?

<bag name="Groups" table="studio_Groups_Subscribers" access="nosetter.camelcase"> 
     <key column="SubscriberId"></key> 
     <many-to-many column="GroupId" class="GroupItem" /> 
</bag> 

và có thể có một thác = "lưu-cập nhật" trên bộ sưu tập SubscriberItems để nhóm sẽ cập nhật cho con của bạn mối quan hệ thích hợp từ "phía bên kia".

+0

jishi: Bạn đã lưu tôi. cảm ơn. Để phần còn lại: Hãy chắc chắn rằng bạn có cả hai lớp tham chiếu lẫn nhau, xác định hai túi trên mỗi bên (đảm bảo rằng một trong hai túi là nghịch đảo, một là không). Và đừng quên đặt tham chiếu trong runtinme: a.bList.Add (b); b.aList.Add (a); – Nils

0

Tôi tin rằng nó là để làm với các tùy chọn thác, kiểm tra bài viết của Ayende trên đó và tôi tưởng tượng với một chút Googling bạn có thể tìm thấy cú pháp chính xác để đi vào tập tin hbm của bạn. Tôi sử dụng fluent-NHibernate vì vậy tôi không thể giúp bạn với các tập tin XML.

http://ayende.com/Blog/archive/2006/12/02/NHibernateCascadesTheDifferentBetweenAllAlldeleteorphansAndSaveupdate.aspx