NHibernate Many-to-Many

I have an outdated database and am trying to create an NHAMNernate DAL. I have a problem displaying the Many-To-Many table.

Database Tables:

  • studio_Subscribers
  • studio_Groups (contains IList subscribers)
  • studio_Subscribers_Groups - Many-to-many table with primary keys

The problem is when I create an instance SubscriberGroupand populate it with subscribers that they save in the table studio_Subscribers, but not in the Many-To-Many table.

I can not understand what happened?

studio_Subscribers Table mapping:

<?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 Table mapping:

<?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>
+5
source share
2 answers

Don't you have the right bag for your subscriber, and the many-to-many relationship with the group?

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

, , cascade = "save-update" SubscriberItems, , " ".

+2

All Articles