流利的nhibernate映射问题

时间:2011-08-10 12:27:28

标签: nhibernate mapping fluent

我有两个对象Customer_policy和policy_maturity。

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字段。我应该做什么类型的映射,以便通过仅一次点击数据库来完成插入和更新?谢谢你的建议。

2 个答案:

答案 0 :(得分: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);
    }
}

然后如果您保存更新的Customerpolicy,NHibernate应批量处理两个更新

答案 1 :(得分:0)

您需要让NHibernate批处理两个DML查询。有关如何在NHibernate中使用批处理的详细说明,请查看this blog post

在事务中包装这两个操作(如博客文章中的示例所示)也是一个好主意。