2012-10-25 4 views
0

아래의 코드와 매핑을 볼 때 왜이 오류가 발생합니까? (부모) 이 오류를 이해하지 못했습니다.

public class Epic : Entity 
{ 
    ... 

    [UsedImplicitly] // NHib uses this 
    public Epic() { 
     _greekHeroes = new HashedSet<GreekHero>(); 
    } 

    /// <summary> 
    /// The backing store for order items. NHib will set this when loaded from the db. 
    /// </summary> 
    private readonly Iesi.Collections.Generic.ISet<GreekHero> _greekHeroes; 

    /// <summary> 
    /// An enumeration of greek heros for public consumption. 
    /// </summary> 
    public virtual IEnumerable<GreekHero> GreekHeroes { get { return _greekHeroes; } } 

    public virtual bool AddItem(GreekHero newItem) 
    { 
     if (!ReferenceEquals(newItem, null) && _greekHeroes.Add(newItem)) 
     { 
      newItem.SetEpic(this); 
      return true; 
     } 
     return false; 
    } 

    public virtual bool RemoveItem(GreekHero itemToRemove) 
    { 
     if (!ReferenceEquals(itemToRemove, null) && _greekHeroes.Remove(itemToRemove)) 
     { 
      itemToRemove.SetEpic(null); 
      return true; 
     } 
     return false; 
    } 
} 

개체 모델 (아이)

public class GreekHero : Entity 
{ 
    ... 

    #endregion 

    #region Epic (a Greek Hero may be associated with an Epic) 

    /// <summary> 
    /// The Epic associated with this Greek Hero. 
    /// </summary> 
    public virtual Epic Epic { get; protected set; } 

    /// <summary> 
    /// This enforces referential integrity in the object model between this <see cref="GreekHero"/> 
    /// and the <see cref="Domain.Epic.GreekHeroes"/>. 
    /// </summary> 
    /// <param name="epic">The new epic associated with this hero.</param> 
    /// <remarks><seealso cref="Domain.Epic.AddItem"/> and <seealso cref="Domain.Epic.RemoveItem"/></remarks> 
    public virtual void SetEpic(Epic epic) 
    { 
     var prevEpic = Epic; 
     if (epic == prevEpic) return; 

     Epic = epic; 

     if (!ReferenceEquals(prevEpic, null)) 
      prevEpic.RemoveItem(this); 

     if (!ReferenceEquals(epic, null)) 
      epic.AddItem(this); 
    } 

    #endregion 
} 

HBM 매핑 (부모)

테스트를
[Test] 
    public void Cascade_Can_Persist_Hero_Without_Epic() 
    { 
     _epic = new Epic("illiad"); 
     var hero = new GreekHero("ted"); 
     hero.SetEpic(_epic); 
     _session.SaveOrUpdate(_epic); 
     _session.Flush(); 
     _session.Evict(_epic); 
     Assert.That(_epic.IsPersistent()); 
     Assert.That(hero.IsPersistent()); 

     var found = _session.Get<GreekHero>(hero.Id); 
     found.Look(); 

     Assert.That(found, Is.EqualTo(hero)); 
    } 

NHibernate: select next_hi from hibernate_unique_key 
NHibernate: update hibernate_unique_key set next_hi = @p0 where next_hi = @p1;@p0 = 6 [Type: Int32 (0)], @p1 = 5 [Type: Int32 (0)] 
NHibernate: select next_hi from hibernate_unique_key 
NHibernate: update hibernate_unique_key set next_hi = @p0 where next_hi = @p1;@p0 = 7 [Type: Int32 (0)], @p1 = 6 [Type: Int32 (0)] 
NHibernate: 
    INSERT INTO Epic (EpicName, EpicId) 
    VALUES (@p0, @p1); 
     @p0 = 'illiad' [Type: String (0)], 
     @p1 = 163840 [Type: Int32 (0)] 
NHibernate: 
    INSERT INTO GreekHero (HeroName, EpicId, GreekHeroId) 
    VALUES (@p0, @p1, @p2); 
     @p0 = 'ted' [Type: String (0)], 
     @p1 = 163840 [Type: Int32 (0)], 
     @p2 = 196608 [Type: Int32 (0)] 
Test 'Core.Data.Tests.NHibernate.Bootstraps.MappingTests.CollectionMappingConstraintTests.Cascade_Can_Persist_Hero_Without_Epic' failed: 
    NHibernate.Exceptions.GenericADOException : could not insert: 
    [Core.TestingSupport.GreekGods.Domain.GreekHero#196608][SQL: INSERT INTO GreekHero (HeroName, EpicId, GreekHeroId) VALUES (?, ?, ?)] 
    ----> System.Data.SqlServerCe.SqlCeException : 
    A foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = FK253126F517FE25F6 ] 

개체 모델을 실패에서

오류
<id name="Id"> 
    <column name="EpicId" /> 
    <generator class="hilo" /> 
</id> 

<natural-id> 
    <property name="EpicName" length="30" /> 
</natural-id> 

<set name="GreekHeroes" 
    cascade="all-delete-orphan" 
    inverse="true" 
    access="field.camelcase-underscore"> 
    <key column="GreekHeroId" /> 
    <one-to-many class="GreekHero"/> 
</set> 

HBM 매핑 된 자식

<id name="Id"> 
    <column name="GreekHeroId" /> 
    <generator class="hilo" /> 
</id> 

<natural-id> 
    <property name="HeroName" length="30" /> 
</natural-id> 

<many-to-one name="Epic" column="EpicId" /> 

답변

1

> 설정 <에서 < 키 > 요소가 자식 테이블 내의 열을 나타내는 곳의 주요 부모이 저장됩니다. 귀하의 경우에는 "EpicId"이어야합니다. 그 이유는 귀하가 < many-to-one >에 진술 한 내용이기 때문입니다.

+0

나는 그것이 어리석은 것을 알았다. 그냥 그게 어리석은 줄을 몰랐다. 감사! – Berryl

관련 문제