开发者

nhibernate many-to-many bag not inserting in the association table

I know this has been asked and answered a number of times, but I have two classes that are many-to-many. I've reciprocal mapped them using bags. Here is the NHibernate Mapping:

Calendar:

<?xml version="1.0" encoding="utf-8"?>
<!--Generated by NHibernate.Mapping.Attributes on 2009-10-09 18:09:29Z.-->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Calendar.Calendar, Calendar" table="Calendars">
    <id name="CalendarId" column="CalendarId" type="Int32">
      <generator class="native" />
    </id>
    <property name="Name" length="100" />
    <property name="CalendarStatus" />
    <property name="CalendarType" type="Calendar.GenericEnumMapper`1[[Calendar.CalendarType, Calendar, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Calendar" length="50" />
    <property name="RecordCreatedBy" column="CreatedBy" length="50" />
    <property name="RecordModifiedBy" column="ModifiedBy" length="50" />
    <property name="CreateDate" />
    <property name="ModifiedDate" />
    <bag name="Events" table="CalendarEventsInCalendar" cascade="all">
      <key column="CalendarId" />
      <many-to-many class开发者_如何转开发="Calendar.CalendarEvent, Calendar" column="CalendarEventId" />
    </bag>
  </class>
</hibernate-mapping>

CalendarEvent:

<?xml version="1.0" encoding="utf-8"?>
<!--Generated by NHibernate.Mapping.Attributes on 2009-10-09 18:27:46Z.-->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Calendar.CalendarEvent, Calendar" table="CalendarEvents">
    <id name="CalendarEventId" column="CalendarEventId" type="Int32">
      <generator class="native" />
    </id>
    <property name="EventStatus" />
    <property name="StartTime" />
    <property name="EndTime" />
    <property name="ImageUrl" length="255" />
    <property name="Description" length="2000" />
    <property name="LocationName" length="255" />
    <property name="Address1" length="255" />
    <property name="Address2" length="255" />
    <property name="City" length="255" />
    <property name="State" length="255" />
    <property name="Zip" length="10" />
    <property name="Title" length="255" />
    <property name="Url" length="255" />
    <property name="ExternalSystemId" />
    <property name="ExternalSystem" type="Calendar.GenericEnumMapper`1[[Calendar.ExternalSystemType, Calendar, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Calendar" length="50" />
    <property name="RecordCreatedBy" column="CreatedBy" length="50" />
    <property name="RecordModifiedBy" column="ModifiedBy" length="50" />
    <property name="CreateDate" />
    <property name="ModifiedDate" />
    <many-to-one name="ParentEvent" column="ParentEventId" />
    <bag name="ChildEvents" lazy="true" fetch="subselect" cascade="all" inverse="true">
      <key column="ParentEventId" />
      <one-to-many class="Calendar.CalendarEvent, Calendar" />
    </bag>
    <bag name="Calendars" table="CalendarEventsInCalendar" lazy="false" inverse="true">
      <key column="CalendarEventId" />
      <many-to-many class="Calendar.Calendar, Calendar" column="CalendarId" />
    </bag>
  </class>
</hibernate-mapping>

And here's how I add an Event to a Calendar:

    public virtual void AddEvent(CalendarEvent calEvent)
    {
        if(!calEvent.Calendars.Contains(this))
            calEvent.Calendars.Add(this);

        Events.Add(calEvent);
    }

Then I do a SaveOrUpdate(calendar)

Any ideas?


You have to wrap the SaveOrUpdate in a transaction and then do Transaction.Commit(). Otherwise, the Mapping doesn't save, only the entities.

Thanks, Jason


I think the problem is in inverse=true for CalendarEvent. You should remove it

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜