2013-06-25 69 views
1

由於標題聲明父級和子級都被插入到數據庫中但未被鏈接。NHibernate級聯不設置外鍵

映射請求

<class name="Request" table="Requests"> 
<id name="RequestId"> 
    <generator class="native" /> 
</id> 
<many-to-one name="BulkReport" class="BulkReport" column="BulkId" cascade="none" lazy="proxy" /> 
<property name="RunNumber" /> 
<property name="MobileNumber" /> 
<property name="Message" /> 
<property name="RequestedOn" /> 
<property name="Closed" /> 

<many-to-one name="Communication" class="Communication" column="CommId" cascade="save-update" lazy="proxy" /> 
<many-to-one name="Outcome" class="Outcome" column="OutcomeId" cascade="none" lazy="proxy" /> 
<many-to-one name="Discount" class="Discount" column="Discount" not-found="ignore" cascade="save-update" lazy="proxy" /> 
</class> 

映射折扣

<class name="Discount" table="Discounts" discriminator-value="BASE"> 
<id name="DiscountId"> 
    <generator class="native" /> 
</id> 

<discriminator column="DiscountType" not-null="true" /> 

<property name="MobileNumber" /> 
<property name="CustomerName" /> 
<property name="AddedOn" /> 
<property name="Status" /> 
<property name="Pending" /> 
<many-to-one name="Product" class="DiscountProduct" column="DiscountProduct" cascade="none" lazy="proxy" /> 
<many-to-one name="AddedBy" class="User" cascade="none" lazy="proxy" /> 

<subclass name="StaffDiscount" discriminator-value="STAFF"> 
    <many-to-one name="Employee" class="Employee" column="EmployeeNumber" cascade="none" lazy="proxy" not-null="false" /> 
</subclass> 
</class> 

SQL從測試

NHibernate: INSERT INTO Discounts (MobileNumber, CustomerName, AddedOn, Status,   Pending, DiscountProduct, AddedBy, DiscountType) VALUES (?, ?, ?, ?, ?, ?, ?, 'BASE');@p0 = 700000000, @p1 = 'Test', @p2 = 25/06/2013 14:38:43, @p3 = True, @p4 = False, @p5 = 1, @p6 = 'Test' 
NHibernate: select @@identity 
NHibernate: INSERT INTO Requests (BulkId, RunNumber, MobileNumber, Message, RequestedOn, Closed, CommId, OutcomeId, Discount) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);@p0 = 1, @p1 = 100, @p2 = 700000000, @p3 = 'Test Request', @p4 = 25/06/2013 14:38:43, @p5 = True, @p6 = NULL, @p7 = 1, @p8 = 0 
NHibernate: select @@identity 

任何想法,爲什麼發生這種情況?在這方面有一個完整的塊?

編輯:請求信息

折扣類別

public class Discount 
{ 
    public virtual int DiscountId { get; set; } 
    public virtual double MobileNumber { get; set; } 
    public virtual string CustomerName { get; set; } 
    public virtual DateTime AddedOn { get; set; } 
    public virtual bool Status { get; set; } 
    public virtual bool Pending { get; set; } 

    public virtual DiscountProduct Product { get; set; } 
    public virtual User AddedBy { get; set; } 

    public override string ToString() 
    { 
     return "0" + MobileNumber + ", " + CustomerName.Trim(); 
    } 
} 

請求類

public class Request 
{ 
    public virtual int RequestId { get; set; } 
    public virtual BulkReport BulkReport { get; set; } 
    public virtual int RunNumber { get; set; } 
    public virtual double MobileNumber { get; set; } 
    public virtual string Message { get; set; } 
    public virtual DateTime RequestedOn { get; set; } 
    public virtual Outcome Outcome { get; set; } 
    public virtual bool Closed { get; set; } 
    public virtual Communication Communication { get; set; } 
    public virtual Discount Discount { get; set; } 
} 

測試我跑

 [Test] 
    public void Can_add_new_request_with_discount() 
    { 
     ISession session = _sessionFactory.OpenSession(); 

     BulkReport report = session.Get<BulkReport>(1); 
     Outcome outcome = session.Get<Outcome>(1); 
     User user = session.Get<User>("Test"); 
     DiscountProduct discountProduct = session.Get<DiscountProduct>(1); 


     Request request = new Request() 
     { 
      Message = "Test Request", 
      BulkReport = report, 
      Closed = true, 
      MobileNumber = 07000000000, 
      RequestedOn = DateTime.Now, 
      RunNumber = 100, 
      Outcome = outcome 
     }; 

     Discount discount = new Discount 
      { 
       AddedBy = user, 
       AddedOn = DateTime.Now, 
       CustomerName = "Test", 
       MobileNumber = 07000000000, 
       Pending = false, 
       Status = true, 
       Product = discountProduct 
      }; 

     request.Discount = discount; 

     session.Save(request); 

     var fromDbRequest = session.Get<Request>(request.RequestId); 
     var fromDbDiscount = session.Get<Discount>(discount.DiscountId); 

     Assert.IsNotNull(fromDbRequest); 
     Assert.IsNotNull(fromDbDiscount); 
     Assert.AreEqual(request.MobileNumber, fromDbRequest.MobileNumber); 
     Assert.AreEqual(discount.MobileNumber, fromDbDiscount.MobileNumber); 

     session.Close(); 
    } 
+1

'逆=「真」'? – valverij

+0

不在我正在使用的模式中?它引入了什麼版本? –

+0

對不起,我剛剛意識到,一個人不能'多對一'。你有Discount.Requests屬性嗎?如果是這樣,它是什麼類型的集合?折扣 - >請求的引用未映射在上面。 – valverij

回答

1

該測試看起來是錯誤的。你真的應該使用交易(並呼籲transaction.Commit())。或者,撥打session.Flush()。如果你這樣做,你很可能會得到一個更新陳述,將Discount設置爲Request

不相關的問題,但你應該打開讓你的fromDb對象的新會話,否則沒有DB提出要求,因爲他們已經在會話的一級緩存

+0

不能相信我沒有看到所有其他測試都是按照您所說的進行的。過於專注於數據庫和映射,而不是代碼。謝謝 –