2011-07-28 11 views
0

私は何か愚かなことをしており、私はそれを見ません。NHibernateマッピング制約違反

次のマッピングを使用して、クラスPartyにPartyNamesを設定し、FK制約違反のPartyNameを挿入できません。必要に応じてオブジェクトコードを投稿しますが、それがマッピングであると思われます。

乾杯、
Berryl

エラーおよびテストコード

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); 
     } 
    } 

マッピング(パーティ)

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

マッピング(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> 

+0

NameSeedsとScopeのコードがないと答えにくいです。 –

+0

@Diego Mijelshon。 Hi Diego、更新された投稿を参照してください。私は双方向の関係を維持するためのコードも追加しました。私はそのSOの質問のために少しのコードを知っています。乾杯 – Berryl

答えて

0

は、まあ、私はそれをマッピングした右だったと私は正しかったです!

 <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> 
関連する問題