views:

3936

answers:

3

Hi Guys.

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.

The Database tables is like this. studio_Subscribers studio_Groups (contains a IList of Subscribers) studio_Subscribers_Groups - Many-To-Many table with primary keys

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.

I cant figure out whats wrong?

Thanks in advance. /Johan

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



<?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>
+1  A: 

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>

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".

jishi
jishi: You saved me. thanks. To the rest: Make sure you have both classes referencing each other, define the two bags on each side (make sure one is inverse, one is not). And don't forget to set the references during runtinme: a.bList.Add(b); b.aList.Add(a);
Nils
A: 

Thanks for your answer, but it didnt help, I still cant get the relation to be persisted in the Many-To-Many table. Its stange beacause if i add an unsaved item to a new unsaved group and saves the group, the group and the subscriber are saved in their tables but the Relation is not?

Any ideas?

/Johan

JWendel
I have been looking at my hbm-files (I'm using ActiveRecord) and I see nothing different except for the thins in my last answer.
jishi
A: 

I believe it is to do with the cascade options, check out Ayende's post on it and I imagine with a bit of Googling you can find the correct syntax to go into your hbm file. I use fluent-NHibernate so I can't help you with the XML file.

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

John_

related questions