Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4073920
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:05:37+00:00 2026-05-20T17:05:37+00:00

I have an employee entry screen for a new employee, that upon submit gets

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:05:38+00:00Added an answer on May 20, 2026 at 5:05 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have employee and department table, employee has foreign key departmentID that is
I have two employee lists that I want to get only unique records from
I have a controller Employee , in that i have the action detail .
I have an employee entity in my EF model. I then added a class
I have employee as a base class with salaried and hourly employees as derived
I have Employee table and an Entity class for it, My task is such
How do I hide the private implementation (implicit sharing) in Qt: I have Employee.cpp
I have an Employee object I am showing in inputtext. For example, the firstname
I have class Employee which is something like this. class Emp { int EmpID;
I have a Employee class with many attributes. One of the attributes is employeeId

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.