开发者

NHibernate mapping constraint violation

I am doing something stupid and I just don't see it.

I just want the class Party to have a set of PartyNames, using the mapping below, and am unable to insert a PartyName with an FK constraint violation. Will post the object code if necessary but suspect it is the mapping.

Chee开发者_Python百科rs,

Berryl

Error and test code

NHibernate: INSERT INTO Parties (Type, PartyId) VALUES ('PERSON', @p0);@p0 = 229376 [Type: Int32 (0)]
NHibernate: INSERT INTO PartyNames (PartyId, RequiredName, EverythingElse, ContextUsed, Salutation, EffectiveStart, EffectiveEnd, PartyNameId) 
    VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7);
    @p0 = 229376 [Type: Int32 (0)], 
    @p1 = 'Hesh' [Type: String (50)], 
    @p2 = 'Berryl;;;' [Type: String (4000)], 
    @p3 = 'Stack Overflow Profile' [Type: String (50)], 
    @p4 = 'Fellow Geek' [Type: String (20)], 
    @p5 = 7/28/2011 1:21:19 PM [Type: DateTime (0)], 
    @p6 = 12/31/9999 11:59:59 PM [Type: DateTime (0)], 
    @p7 = 262144 [Type: Int32 (0)]
Test 'Parties.Data.Impl.NHib.Tests.TestFixtures.SqlServerCommandExecutor.GenerateTestData' failed: NHibernate.Exceptions.GenericADOException : could not insert: [Parties.Domain.Names.PartyName#262144][SQL: INSERT INTO PartyNames (PartyId, RequiredName, EverythingElse, ContextUsed, Salutation, EffectiveStart, EffectiveEnd, PartyNameId) VALUES (?, ?, ?, ?, ?, ?, ?, ?)]
  ----> System.Data.SqlClient.SqlException : The INSERT statement conflicted with the FOREIGN KEY constraint "Party_PartyName_FK". The conflict occurred in database "PartyDomainDb", table "dbo.Parties", column 'PartyId'.

    [Test, Explicit]
    public void GenerateTestData()
    {
        NameSeeds.Initialize();

        var party = new Person();

        using (Scope(true))
        {
            _session.SaveOrUpdate(party);
            NameSeeds.DevName.Party = party;
            NameSeeds.LegalName.Party = party;
            Assert.That(party.Names.Count(), Is.EqualTo(2));
            _session.SaveOrUpdate(party);
        }
    }

Mapping (Party)

   <class name="Party" table="Parties">
    <id name="Id">
      <column name="PartyId" />
      <generator class="hilo" />
    </id>

    <discriminator column="Type" not-null="true" type="string" />

    <set access="field.camelcase-underscore" cascade="all-delete-orphan" inverse="true" name="Names">
      <key foreign-key="Party_PartyName_FK">
        <column name="PartyNameId" />
      </key>
      <one-to-many class="Parties.Domain.Names.PartyName, Parties.Domain" />
    </set>

    <subclass 
      name="Parties.Domain.People.Person, Parties.Domain" 
      discriminator-value="PERSON"
      >

    </subclass>

Mapping (PartyName)

<class name="PartyName" table="PartyNames">
<id name="Id">
  <column name="PartyNameId" />
  <generator class="hilo" />
</id>

<natural-id>
  <many-to-one access="field.camelcase-underscore" class="Parties.Domain.Party" foreign-key="Party_FK" name="Party" cascade="save-update">
    <column name="PartyId" index="PartyIndex" not-null="true" />
  </many-to-one>
  <property name="RequiredName" not-null="true" length="50"/>
</natural-id>

<property name="EverythingElse" />
<property name="ContextUsed" length="50"/>
<property name="Salutation" length="20"/>
<property name="EffectivePeriod" type="Smack.Core.Data.NHibernate.UserTypes.DateRangeUserType, Smack.Core.Data">
  <column name="EffectiveStart"/>
  <column name="EffectiveEnd"/>
</property>


Well I was right it was the mapping and I was right that it was stupid

I had the column name on the foreign key mixed up!

        <set access="field.camelcase-underscore" cascade="all-delete-orphan" inverse="true" name="Names">
      <key foreign-key="Party_PartyName_FK">
        <column name="PartyNameId" />    ** WRONG, what I had
        <column name="PartyId" />        ** RIGHT, what I umm, meant
      </key>
      <one-to-many class="Parties.Domain.Names.PartyName, Parties.Domain" />
    </set>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜