2011-07-28 218 views
0

我正在做一些愚蠢的事情,我只是没有看到它。NHibernate映射约束违规

我只想让Party 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。嗨迭戈,看到更新后的帖子。我还加入了维护双向关系的代码 - 我知道它为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>