views:

13

answers:

0

I have a class hierarchy - Document as supper-class and Paper, Presentation and Report as sub-classes. I choose table-per-subclass strategy for the mapping. see the mapping bellow:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model">
  <class name="Document" abstract="true" table="Document" lazy="false">
    <id name="Id" column="Id">
      <generator class="guid"/>
    </id>
    <many-to-one name="Project" column="ProjectId" not-null="true"/>
    <joined-subclass name="Paper" table="Paper">
      <key column="Id"/>
    </joined-subclass>
    <joined-subclass name="Presentation" table="Presentation">
      <key column="Id"/>
    </joined-subclass>
    <joined-subclass name="Report" table="Report">
      <key column="Id"/>
    </joined-subclass>
  </class>
</hibernate-mapping>

please pay attention to many-to-one association to Project class. now the mapping for Project:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Model" namespace="Model">
  <class name="Project" table="Project" lazy="false">
    <id name="Id" column="Id">
      <generator class="guid"/>
    </id>
    <bag name="Papers" inverse="true" lazy="true" cascade="all">
      <key column="Id"/>
      <one-to-many class="Paper"/>
    </bag>
    <bag name="Presentations" inverse="true" lazy="true" cascade="all">
      <key column="Id"/>
      <one-to-many class="Presentation"/>
    </bag>
    <bag name="Reports" inverse="true" lazy="true" cascade="all">
      <key column="Id"/>
      <one-to-many class="Report"/>
    </bag>
  </class>
</hibernate-mapping>

I want all the Documents be deleted when I delete a Project

ISession.Delete(Project);

but as the statement above is executed I receive the following:

The DELETE statement conflicted with the REFERENCE constraint "FK_Project_Document". The conflict occurred in database "None", table "dbo.Document", column 'ProjectId'.
The statement has been terminated.

could you please tell me what's wrong with the mapping?

related questions