1

我有一些問題級聯所有(孤兒),並從數據庫中刪除舊的objcet。流利的NHibernate參考/多對一映射級聯全部不工作

例如: 我有一個類A包含一個類B的對象。現在,當我創建一個類A的對象並保存它,一切工作正常。當我調用方法SetValueOfB(int i)並再次保存對象A時,舊對象B仍在數據庫中。

這些類之間的關聯是否必須是定向的(對於每個HasMany/Reference/HasOne ...)? (但對象b沒有任何關於對象a的知識)

有沒有辦法解決單向關聯的問題?

我需要一對一映射嗎?因爲對象B只能屬於對象A(A是一個參數,B是一個值)。

這是一個失敗的測試:

using System.Collections.Generic; 
using System.Data; 
using System.IO; 
using FluentNHibernate.Cfg; 
using FluentNHibernate.Cfg.Db; 
using FluentNHibernate.Mapping; 
using NHibernate; 
using NHibernate.Cfg; 
using NHibernate.Tool.hbm2ddl; 
using NUnit.Framework; 

namespace ReferenceCascade.Test 
{ 
public class CascadeTest 
{ 
    private const string DbFile = "firstProject.db"; 

    [Test] 
    public void checkCascadeAll() 
    { 
     var sessionFactory = CreateSessionFactory(); 

     A testee = new A(new B(1)); 

     using (var session = sessionFactory.OpenSession()) 
     { 
      using (var transaction = session.BeginTransaction()) 
      { 

       session.SaveOrUpdate(testee); 

       transaction.Commit(); 
      } 
     } 

     testee.SetValueOfB(2); 

     using (var session = sessionFactory.OpenSession()) 
     { 
      using (var transaction = session.BeginTransaction()) 
      { 
       session.SaveOrUpdate(testee); 

       transaction.Commit(); 
      } 
     } 

     using (var session = sessionFactory.OpenSession()) 
     { 
      using (session.BeginTransaction()) 
      { 
       IList<B> stores = session.CreateCriteria(typeof(B)) 
        .List<B>(); 

       Assert.That(stores.Count, Is.EqualTo(1)); 
      } 
     } 
    } 

    private static ISessionFactory CreateSessionFactory() 
    { 
     return Fluently.Configure() 
      .Database(SQLiteConfiguration.Standard.UsingFile(DbFile).IsolationLevel(IsolationLevel.ReadCommitted)) 
      .Mappings(m => 
      m.FluentMappings.AddFromAssemblyOf<CascadeTest>()) 
      .ExposeConfiguration(BuildSchema) 
      .BuildSessionFactory(); 
    } 

    private static void BuildSchema(Configuration config) 
    { 
     // delete the existing db on each run 
     if (File.Exists(DbFile)) 
     { 
      File.Delete(DbFile); 
     } 

     // this NHibernate tool takes a configuration (with mapping info in) 
     // and exports a database schema from it 
     new SchemaExport(config) 
      .Create(false, true); 
    } 
} 

public abstract class Entity 
{ 
    public const long InitialId = 0; 

    private readonly long _id; 

    protected Entity() 
    { 
     _id = InitialId; 
    } 

    public virtual long Id 
    { 
     get { return _id; } 
    } 
} 

public class A : Entity 
{ 
    private B _b; 

    public A() 
    { 
    } 

    public A(B b) 
    { 
     _b = b; 
    } 

    public virtual void SetValueOfB(int i) 
    { 
     _b = new B(i); 
    } 

    public virtual B B 
    { 
     get { return _b; } 
    } 
} 

public class B : Entity 
{ 
    private readonly int _i; 

    public B() 
    { 
    } 

    public B(int i) 
    { 
     _i = i; 
    } 

    public virtual int I 
    { 
     get { return _i; } 
    } 
} 

public class EntityMap<T> : ClassMap<T> where T : Entity 
{ 
    public EntityMap() 
    { 
     Id(x => x.Id).GeneratedBy.HiLo("33878").Access.CamelCaseField(Prefix.Underscore); 
    } 
} 

public class AMap : EntityMap<A> 
{ 
    public AMap() 
    { 
     Table("A"); 
     References(x => x.B).Not.LazyLoad().Cascade.All().Access.CamelCaseField(Prefix.Underscore); 
    } 
} 

public class BMap : EntityMap<B> 
{ 
    public BMap() 
    { 
     Table("B"); 
     Map(x => x.I).Not.LazyLoad().Access.CamelCaseField(Prefix.Underscore); 
    } 
} 

}

還是這裏的項目:vs project

+0

如果B被綁定到A爲什麼你有它在單獨的表?遺產? 'cascade.DeleteOrphan'是你在這裏需要的,但是這隻適用於兒童的集合,因爲它對於引用沒有意義。 – Firo

回答