1

我有3類和它們的映射和他們每個人都有參考先前的類似:連貫NHibernate一到多用3類

A - >乙 - 「ç

A可以具有Ç但只有當B獲得了C(由於其不可能A - > C)

class A 
{ 
    public virtual int Id {get;set;} 
    public virtual string messageA {get;set;} 
    public virtual IList<B> Bs {get;set;} 
} 


class B 
{ 
    public virtual int Id {get;set;} 
    public virtual string messageB {get;set;} 
    public virtual A A {get; set;} 
    public virtual IList<C> Cs {get;set;} 
} 

class C 
{ 
    public virtual int Id {get;set;} 
    public virtual string messageC {get;set;} 
    public virtual B B{get;set;} 
} 

//Mappings 

class AMap : ClassMap<A> 
{ 
    public AMap() 
    { 
     Id(x => x.Id); 
     Map(x => x.messageA); 
     HasMany(x => x.Bs); 
    } 
} 

class BMap : ClassMap<B> 
{ 
    public BMap() 
    { 
     Id(x => x.Id); 
     Map(x => x.messageB); 
     References(x => x.A).Cascade.All(); 
     HasMany(x => x.Cs); 
    } 
} 

class CMap : ClassMap<C> 
{ 
    public CMap() 
    { 
     Id(x => x.Id); 
     Map(x => x.messageC); 
     References(x => x.B).Cascade.All(); 
    } 
} 

我試圖犯這樣的錯誤:

對象引用的未保存瞬態實例 - 在沖洗之前保存瞬態 實例,或者將屬性的級聯操作設置爲 ,這會使其自動保存。類型:NHibernateTest.C,實體: NHibernateTest.C。

我一直在尋找解決方案,有人說我要修改外鍵更新規則爲級聯,我做到了,仍然沒有工作。

我只是想提一下,它正在與A - > B關係,當我添加C出現錯誤。

回答

1

在將來避免這樣的問題,你應該確保:

  • 您的所有課程都是公共
  • 不必循環引用,如你的例子B-> A,C->如您所願(A-> B-> c)將b

以下測試案例工程。

模型定義:

public class A 
{ 
    public virtual int Id { get; set; } 
    public virtual string messageA { get; set; } 
    public virtual IList<B> Bs { get; set; } 
} 


public class B 
{ 
    public virtual int Id { get; set; } 
    public virtual string messageB { get; set; } 
    public virtual IList<C> Cs { get; set; } 
} 

public class C 
{ 
    public virtual int Id { get; set; } 
    public virtual string messageC { get; set; } 
} 

地圖定義:

public class AMap : ClassMap<A> 
{ 
    public AMap() 
    { 
     Table("A"); 
     Id(x => x.Id).Column("Id"); 
     Map(x => x.messageA); 
     HasMany(x => x.Bs).Cascade.All(); 
    } 
} 

public class BMap : ClassMap<B> 
{ 
    public BMap() 
    { 
     Table("B"); 
     Id(x => x.Id).Column("Id"); ; 
     Map(x => x.messageB); 
     HasMany(x => x.Cs).Cascade.All(); 
    } 
} 

public class CMap : ClassMap<C> 
{ 
    public CMap() 
    { 
     Table("C"); 
     Id(x => x.Id).Column("Id"); ; 
     Map(x => x.messageC); 
    } 
} 

NUnit測試方法:

[Test] 
public void ShouldCorrectlyMapA() 
{ 
    var objectA = new A 
    { 
     Id = 1, 
     messageA = "Message A", 
     Bs = new List<B> { 
      new B 
      { 
       messageB = "Message B", 
       Cs = new List<C> 
       { 
        new C 
        { 
         messageC = "Message C" 
        } 
       } 
      } 
     } 
    }; 

    new PersistenceSpecification<A>(_session) 
     .VerifyTheMappings(objectA); 

} 

使用in-memory SQLite單元測試,

模式創建的測試運行器輸出:

NHibernate: INSERT INTO A (messageA) VALUES (@p0); select last_insert_rowid();@p0 = 'Message A' [Type: String (0)] 
NHibernate: INSERT INTO B (messageB) VALUES (@p0); select last_insert_rowid();@p0 = 'Message B' [Type: String (0)] 
NHibernate: INSERT INTO C (messageC) VALUES (@p0); select last_insert_rowid();@p0 = 'Message C' [Type: String (0)] 
NHibernate: UPDATE B SET A_id = @p0 WHERE Id = @p1;@p0 = 1 [Type: Int32 (0)], @p1 = 1 [Type: Int32 (0)] 
NHibernate: UPDATE C SET B_id = @p0 WHERE Id = @p1;@p0 = 1 [Type: Int32 (0)], @p1 = 1 [Type: Int32 (0)] 
:在剛剛創建的模式

create table A (
    Id integer primary key autoincrement, 
    messageA TEXT 
) 

create table B (
    Id integer primary key autoincrement, 
    messageB TEXT, 
    A_id INT, 
    constraint FKCDCAB7DE9EFDCD1D foreign key (A_id) references A 
) 

create table C (
    Id integer primary key autoincrement, 
    messageC TEXT, 
    B_id INT, 
    constraint FKCDCAB7DD3844B1E1 foreign key (B_id) references B 
) 

DML操作