2011-08-10 109 views
0

我有兩個對象Customer_policy和policy_maturity。流利的nhibernate映射問題

public class Customer_policy 
{ 
    public virtual String policy_no { get; set; } 
    public virtual DateTime maturity_date { get; set; } 
    public virtual Boolean matured_status { get; set; } 
} 

public class Policy_maturity 
{ 
    public virtual string policy_no { get; set; } 
    public virtual string customer_id { get; set; } 
    public virtual float maturity_amt { get; set; } 
    public virtual string policy_type { get; set; } 
    public virtual DateTime pay_date { get; set; } 
} 

當客戶創建策略Customer_policy正在填充,但Policy_maturity應該保持爲空(我已經完成)。當策略成熟時,我想在Policy_maturity中插入一行,並更新相應Customer_policy的matured_status字段。我應該做什麼類型的映射,以便只通過一次點擊數據庫就可以完成插入和更新?感謝您的建議。

回答

0

您將需要問NHibernate批量兩個DML查詢。有關如何在NHibernate中使用批處理的詳細說明,請參見this blog post

在交易中包裝這兩個操作(如博客文章中的示例)也是一個好主意。

1

一個建議:

public class CustomerPolicy 
{ 
    public virtual String PolicyNumber { get; private set; } 
    public virtual DateTime MaturityDate { get; set; } 
    public virtual PolicyMaturity Maturity { get; private set; } 

    public virtual Boolean HasMatured { get { return Maturity != null; } } 

    public virtual void DoMature(Customer customer, float maturity_amt, string policyType, DateTime payDate) 
    { 
     DoMature(new PolicyMaturity 
     { 
      Customer = customer, 
      MaturityAmt = maturity_amt, 
      PolicyType = policyType, 
      PayDate = payDate, 
     }); 
    } 
    /*public*/ virtual void DoMature(PolicyMaturity maturity) 
    { 
     Maturity = maturity; 
     MaturityDate = DateTime.Today; 
    } 
} 

public class PolicyMaturity 
{ 
    public virtual String PolicyNumber { get; private set; } 
    public virtual CustomerPolicy Policy { get; set; } 
    public virtual Customer Customer { get; set; } 
    public virtual float MaturityAmt { get; set; } 
    public virtual string PolicyType { get; set; } 
    public virtual DateTime PayDate { get; set; } 
} 

class CustomerPolicyMap : ClassMap<CustomerPolicy> 
{ 
    public CustomerPolicyMap() 
    { 
     Id(cp => cp.PolicyNumber).GeneratedBy.Assigned(); 

     Map(cp => cp.MaturityDate); 
     HasOne(cp => cp.Maturity); 
    } 
} 

class PolicyMaturityMap : ClassMap<PolicyMaturity> 
{ 
    public PolicyMaturityMap() 
    { 
     Id(cp => cp.PolicyNumber).GeneratedBy.Foreign("Policy"); 

     HasOne(cp => cp.Policy); 

     References(cp => cp.Customer); 
     Map(cp => cp.MaturityAmt); 
     Map(cp => cp.PayDate); 
     Map(cp => cp.PolicyType); 
    } 
} 

然後如果你保存更新NHibernate的Customerpolicy應該批兩個更新