为什么不使用此NHibernate配置文件删除级联?

时间:2011-09-27 14:10:19

标签: nhibernate nhibernate-mapping

我有以下的NHibernate文件,但由于两个答案表中的任何一个都有外键约束,因此我不允许删除问题。删除相应的问题并在Answers元素上设置级联设置后,所需的行为是删除答案。

以下是配置文件,任何人都可以看到问题所在

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" namespace="Test.Domain" assembly="Test.Domain" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Question" abstract="true">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <discriminator />
    <property name="Text" length="500" />
    <property name="Note" length="2000" />
    <property name="DateRun" />
    <many-to-one name="ChoiceType" column="ChoiceTypeId" />
    <bag name="Answers" inverse="true" cascade="all,delete-orphan">
      <key column="QuestionId" on-delete="cascade" />
      <one-to-many class="Answer" />
    </bag>
  </class>
  <class name="Answer" abstract="true">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <many-to-one name="Question" column="QuestionId" />
    <many-to-one name="Group" column="GroupId" />
    <property name="Comment" />
  </class>
  <class name="Choice">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <property name="Text" length="500" />
  </class>
  <class name="ChoiceType">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <property name="Name" />
    <property name="Type" />
    <list name="Choices" cascade="all,delete-orphan">
      <key column="ChoiceTypeId" />
      <list-index column="ChoicesPos" />
      <one-to-many class="Choice" />
    </list>
  </class>
  <class name="Division">
    <id name="Id" type="Int32">
      <generator class="hilo" />
    </id>
    <property name="Name" />
  </class>
  <union-subclass name="FreeTextAnswer" extends="Answer">
    <property name="Text" length="500" />
  </union-subclass>
  <union-subclass name="MultipleChoiceAnswer" extends="Answer">
    <many-to-one name="Choice" column="ChoiceId" />
  </union-subclass>
  <subclass name="MultipleChoiceQuestion" extends="Question" />
  <subclass name="FreeTextQuestion" extends="Question" />
</hibernate-mapping>

2 个答案:

答案 0 :(得分:1)

不是吗     级联= “全删除-孤儿”

...用连字符,而不是两个枚举值?

答案 1 :(得分:0)

您的答案集合映射应如下所示:

<bag name="Answers" inverse="true" cascade="all-delete-orphan">
  <key column="QuestionId"/>
  <one-to-many class="Answer" />
</bag>

我删除了on-delete="cascade"。当你想从问题中删除一个答案时,你将不得不“追逐指针”。将问题引用设置为NULL,并将其从答案集合中删除:

answer.Question = null;
相关问题