0
votes

I have two objects Customer_policy and 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; }
}

When a customer creates a policy Customer_policy is getting populated but Policy_maturity should remain empty(which I have already done). When a policy matures I want to insert a row in Policy_maturity and update matured_status field of the corresponding Customer_policy. What type of mapping should I do so that inserting and update gets accomplished by hitting the database only one time ??? Thanx for your suggestions.

2

2 Answers

1
votes

a suggestion:

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);
    }
}

then if you save the Updated Customerpolicy NHibernate should batch the two updates

0
votes

You will need to ask NHibernate to batch the two DML queries. Have a look at this blog post for a detailed description of how to use batching in NHibernate.

Wrapping the two operations in a transaction (as the example in the blog post does) is also a good idea.