nhibernate多对多包不插入关联表

时间:2009-10-10 01:35:05

标签: nhibernate many-to-many

我知道这已被多次询问和回答,但我有两个多对多的课程。我用袋子互相映射它们。这是NHibernate映射:

日历:

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

以下是我如何向日历添加活动:

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

        Events.Add(calEvent);
    }

然后我做了SaveOrUpdate(日历)

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

您必须将SaveOrUpdate包装在事务中,然后执行Transaction.Commit()。否则,映射不会仅保存实体。

谢谢, 杰森

答案 1 :(得分:0)

我认为问题出现在CalendarEvent的inverse=true中。你应该删除它