Question

I have a EmployeeMonth object which saves the calculated Bonus and Points earned by an employee. In the EmployeeMonth object there is a BonusMonth object. The BonusMonth object sets the intervals which the employee must reach every month to earn bonus (This object is set each month and should not be changed).

When i re-calculate bonus with a reduction eg: 88%, i adjust the BonusMonth object intervals and then recalculate the bonus. The new bonus (result) is then saved in the same EmployeeMonth object.

When i then save the new EmployeeMonth, the BonusMonth is saved as-well.

How do i configure the nhibernate mapping so the BonusMonth is not saved? I only want to update/save the re-calculated EmployeeMonth values.

This is how my mapping looks like:

EmployeeMonth.cs

public class EmployeeMonth
{
    public virtual Guid Id { get; private set; }
    public virtual BonusMonth BonusMonth { get; set; }
    public virtual Employee Employee { get; set; }
    public virtual int WorkReductionPercent { get; set; }
    public virtual int Points { get; set; }
    public virtual decimal Bonus{ get; set; }
}

BonusMonth.cs

public class BonusMonth
{
    public virtual int BasicPoints { get; set; }
    public virtual int ExtraPoints { get; set; }
    public virtual int MaxPoints { get; set; }

    public virtual decimal BasicBonus { get; set; }
    public virtual decimal ExtraBonus { get; set; }
    public virtual decimal MaxBonus { get; set; }
}

EmployeeMonth.hbm.xml

<class name="EmployeeMonth">
    <id name="Id">
        <generator class="guid" />
    </id>

    <property name="WorkReductionPercent"/>
    <property name="Points"/>
    <property name="Bonus"/>

    <many-to-one name="Employee" unique-key="EpId" column="EmployeeId" cascade="none" not-null="true" insert="true" update="false" />
    <many-to-one name="BonusMonth" column="BonusMonthId" cascade="none" not-null="true" insert="true" update="false" />
</class>

BonusMonth.hbm.xml

<class name="BonusMonth">
    <id name="Id">
        <generator class="guid" />
    </id>
    <property name="BasicPoints"/>
    <property name="ExtraPoints"/>
    <property name="MaxPoints"/>
    <property name="BasicBonus"/>
    <property name="ExtraBonus"/>
    <property name="MaxBonus"/>
</class>
Was it helpful?

Solution

It's got nothing to do with the cascading - it's because the BonusMonth entity is associated with the NHibernate session. I would personally recommend not changing the BonusMonth entity unless you want the changes to persist to the database, however if you insist, you can work around the issue by detaching the BonusMonth entity from the session:

session.Evict(employeeMonth.BonusMonth)

You will still need to have the cascade="none" as otherwise it will just re-attach the entity when you save the employeeMonth.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top