views:

336

answers:

1

Currently I have database with the following associations:

  • One Client to Many Intakes
  • One Intake to Many CaseManagements
  • One CaseManagement to Many Interventions
  • Client, Intake, CaseManagement are single classes per table
  • Intervention is a class-hierarchy-per-table.

Currently, if I do something like this:

var client = new Client();
clientRepo.Add(client);

var intake = new Intake();
client.Add(intake);

var caseMan = new CaseManagement();
intake.Add(caseMan);

clientRepo.Update(client);

Everything works fine, and NHibernate creates a Client, then an Intake and then a CaseManagement in the database (all appropriately linked).

However, if do the following:

var client = new Client();
clientRepo.Add(client);

var intake = new Intake();
client.Add(intake);

var caseMan = new CaseManagement();
intake.Add(caseMan);

var intervention = new SubIntervention();
caseMan.Add(intervention);

clientRepo.Update(client);

It screwed up and runs the following SQL:

INSERT INTO TblClient ... (*)
INSERT INTO TblIntake ...
INSERT INTO TblCaseManagement ...
INSERT INTO TblClient ... (*)

Where the starred lines are identical. I have no clue why using inheritance is causing this.

Here is my mapping for my Intake class (which is pretty much the same as Client and CaseManagement).

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="WebTracker4"
                   namespace="WebTracker4.Domain">

<!-- Class Mapping -->
<class name="Intake" table="TblIntake">

<!-- Id -->
<id name="Id" column="IntakeId">
  <generator class="sequence">
    <param name="sequence">IntakeSequence</param>
  </generator>
</id>

<!-- TCN -->
<version name="Tcn" column="IntakeTcn" type="Int64" />

<!-- Client -->    
<many-to-one name="Client" column="ClientId" class="Client" />

<!-- Case Management -->    
<bag name="CaseManagements" inverse="true" cascade="all" table="TblCaseManage" order-by="CaseManageId desc">
  <key column="IntakeId" />
  <one-to-many class="CaseManagement" />
</bag>

<!-- Properties -->
...

</class>
</hibernate-mapping>

Here's my mapping for the Intervention class hierarchy:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               assembly="WebTracker4"
               namespace="WebTracker4.Domain.Interventions">

<!-- Class Mapping -->
<class name="Intervention" table="TblIntervention" abstract="true">

<!-- Id -->
<id name="Id" column="InterventionId">
  <generator class="sequence">
    <param name="sequence">InterventionSequence</param>
  </generator>
</id>

<!-- Discriminator -->
<!-- This is used so that we can have Intervention subclasses. -->
<discriminator column="InterventionType" type="String" />

<!-- TCN -->
<version name="Tcn" column="InterventionTcn" type="Int64" />  

<!-- Case Management -->
<many-to-one name="CaseManagement" column="CaseManageId" class="WebTracker4.Domain.CaseManagement, WebTracker4" />

<!-- Properties -->    
....

<!-- Assessment Subclass -->
<subclass name="SubIntervention" discriminator-value="Sub">
  ...
</subclass>

</class>

What am I missing that is making it try to re-add the Client entity? What also may be of note is that if I screw up the column names in the subclass, NHibernate doesn't say anything.

This is driving me crazy, please help :)

A: 

I figured out the problem. It was because I was using the InterventionType column as both the discriminator and a property. I found that if I changed

<discriminator column="InterventionType" type="String" />

into

<discriminator column="InterventionType" type="String" insert="false" />

the problem went away.

I think it's because you're basically telling NHibernate not to manage the discriminator column.

cdmckay