1

I have an employee entry screen for a new employee, that upon submit gets intercepted by an employee model binder. An employee has a "business unit" and an "override business unit". The override business unit was a recently added and is the cause of my problems.

Here is a partial employee mapping:

 <class name="Employee" table="Employee">
    <id name="Id" column="id">
      <generator class="native" />
    </id>
    ...
    <many-to-one name="BusinessUnit" class="BusinessUnit" column="businessUnitId" />
    <many-to-one name="OverrideBusinessUnit" class="BusinessUnit" column="overrideBusinessUnitId" not-null="false" />
    ...
   </class>

Here is the business unit mapping:

<class name="BusinessUnit" table="BusinessUnit" lazy="true">
    <id name="Id" column="id">
        <generator class="native" />
    </id>
    <property name="Description" column="description"/>
    <many-to-one name="guidelineApprover" class="Employee" column="guidelineApproverId" cascade="none" fetch="join" access="field" />
    <many-to-one name="increaseApprover" class="Employee" column="increaseApproverId" cascade="none" fetch="join" access="field" />
 </class>

In the employee model binder after submitting the form, I am using NHibernate to get both the business unit and the override business unit from the database. This is the code:

Fetching the business unit from the database in the model binder:

   string attemptedBusinessUnitId = valueProvider.GetValue(key).AttemptedValue;
   Int32.TryParse(attemptedBusinessUnitId, out businessUnitId);
   employee.BusinessUnit = businessUnitRepository.Get(businessUnitId);
   modelState.SetModelValue(key, valueProvider.GetValue(key));

Fetching the override business unit from the database in the model binder:

    string attemptedOverrideBusinessUnitId = valueProvider.GetValue(key).AttemptedValue;
    Int32.TryParse(attemptedOverrideBusinessUnitId, out overrideBusinessUnitId);
    employee.OverrideBusinessUnit = businessUnitRepository.Get(overrideBusinessUnitId);
    modelState.SetModelValue(key, valueProvider.GetValue(key));

My current fetch mode is set to "commit". My issue is that I started to get the following error after I added the "override business unit" many-to-one and I attempt to execute employeeRepository.Save(employee):

    object references an unsaved transient instance - save the transient instance before flushing. 
Type: BusinessUnit, Entity: BusinessUnit

If I set the cascade="all" on this field, I just get another similar exception but about needing to save the transient instance of type Employee, entity EmployeeEmpty. Can anyone tell me by looking at the code snippets how I can avoid this exception (preferably without involving cascade)?

Edit:

employeeRepository.Save(employee) just calls session.SaveOrUpdate(employee). So all I am trying to do is to save the employee after assigning to it, the two business unit fields that I retrieved from the database.

SideFX
  • 839
  • 1
  • 12
  • 34

2 Answers2

1

Sorry. I figured it out. It was actually an issue with the BusinessUnit properties in the Employee class. I'm using the null object pattern, so I was getting this error because NHibernate was trying to save a transient object of type "BusinessUnit.Empty". This was due to a bug in my code caused by failing to correctly convert between BusinessUnit.Empty and null. Thanks for all the help anyways.

This is the method that I'm using when implementing the null object pattern. First set access="field" on the NHibernate properties in the mapping file. This will allow NHibernate to read the value from the private variable instead of the public property. e.g.

<many-to-one name="businessUnit" class="BusinessUnit" column="businessUnitId" cascade="none" access="field" />

Then do something like this in your class:

private BusinessUnit businessUnit;
public virtual BusinessUnit BusinessUnit 
{
    get
    {
        if(businessUnit == null)
            return BusinessUnit.Empty;

        return businessUnit;
    }
    set { 
        if (value == BusinessUnit.Empty)
            value = null;

        businessUnit = value;
    }
}

As you can see, the private variable will never be assigned a value of BusinessUnit.Empty. NHibernate will always see the value "null" instead. This pattern is what fixed my issues. Hope it will eventually be helpful to someone else.

SideFX
  • 839
  • 1
  • 12
  • 34
0

If you are creating a new employee in the form, then updating the BusinessUnit and OverrideBusinessUnit, then the following holds true :

  1. You donot need to use cascadeType=All, as you are donot need to save the instances of the bussiness units.
  2. What does the employeeRepository.SaveOrUpdate(employee) do? If it tries to update the employee instance, then you will get the error as you have mentioned. You should try saving this employee object.

If my asumptions are wrong, please post more code snippets.

Satadru Biswas
  • 1,555
  • 2
  • 13
  • 23
  • I added a little more information. Does this have anything to do with there being 2 many-to-one's for the same class,with both being rehydrated from the database and assigned to the employee. I didn't have this issue when I was just dealing with the "businessUnit" many-to-one. This issue was only introduced when I added the "businessUnitOverride" many-to-one. – SideFX Mar 15 '11 at 12:48
  • I dont think that might be a problem, looks ok in sense of mapping. But more than one many-to-one mapping to the same class is not a good idea in sense of design. Since they are already many why would you want then to be in two seperate lists? – Satadru Biswas Mar 15 '11 at 13:33
  • Also the mapping is pretty confusing. From the mapping file I can see that there exists a Many-to-Many relationship between the employee and bussiness unit (the guidelineApprover and increaseApprover). I will recommend you to revist the mapping once again, even if you manage to get out of this error without doing so, it might eventually cause problems. In this case you might want GuidelineApprover and IncreaseApprover to extend Employee and then make on Many-to-Many mapping on each from BussinessUnit. Same should be done on the Employee side for bussinessUnit and overrideBussinessUnit. – Satadru Biswas Mar 15 '11 at 13:43
  • 1
    Edit to the comment: GuidelineApprover and IncreaseApprover should be one-to-many with Business Unit. Similar on the employee side. As for the error, you are clearly saving some that dont exist, so you should look into the complete sequence of events related to this update. Do not use hibernate's saveOrUpdate rather go for merge. – Satadru Biswas Mar 15 '11 at 14:36
  • See this for reference to merge() http://stackoverflow.com/questions/5311928/hibernate-envers-merge-saveorupdate – Satadru Biswas Mar 15 '11 at 14:39