开发者

Object references an unsaved transient instance issue

开发者 https://www.devze.com 2023-02-17 07:35 出处:网络
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 bus

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 us开发者_JS百科ing 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消