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 1049427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:36:54+00:00 2026-05-16T16:36:54+00:00

I get this error: a different object with the same identifier value was already

  • 0

I get this error:

a different object with the same
identifier value was already
associated with the session: 63, of
entity: Core.Domain.Model.Employee

Within my ASP.NET MVC controller actions I do this:

public ActionResult Index(int? page)
{
    int totalCount;
    Employee[] empData = _employeeRepository.GetPagedEmployees(page ?? 1, 5, out totalCount);

    EmployeeForm[] data = (EmployeeForm[]) Mapper<Employee[], EmployeeForm[]>(empData);

    PagedList<EmployeeForm> list = new PagedList<EmployeeForm>(data, page ?? 1, 5, totalCount);


    if (Request.IsAjaxRequest())
        return View("Grid", list);

    return View(list);
}

public ActionResult Edit(int id)
{
    ViewData[Keys.Teams] = MvcExtensions.CreateSelectList(
        _teamRepository.GetAll().ToList(), 
        teamVal => teamVal.Id, 
        teamText => teamText.Name);
    return View(_employeeRepository.GetById(id) ?? new Employee());
}

[HttpPost]
public ActionResult Edit(
    Employee employee, 
    [Optional, DefaultParameterValue(0)] int teamId)
{
    try
    {
        var team = _teamRepository.GetById(teamId);
        if (team != null)
        {
            employee.AddTeam(team);
        }

        _employeeRepository.SaveOrUpdate(employee);

        return Request.IsAjaxRequest() ? Index(1) : RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

Mapping files:

Employee

public sealed class EmployeeMap : ClassMap<Employee>
{
    public EmployeeMap()
    {
        Id(p => p.Id)
            .Column("EmployeeId")
            .GeneratedBy.Identity();

        Map(p => p.EMail);
        Map(p => p.LastName);
        Map(p => p.FirstName);

        HasManyToMany(p => p.GetTeams())
            .Access.CamelCaseField(Prefix.Underscore)
            .Table("TeamEmployee")
            .ParentKeyColumn("EmployeeId")
            .ChildKeyColumn("TeamId")
            .LazyLoad()
            .AsSet()
            .Cascade.SaveUpdate();

        HasMany(p => p.GetLoanedItems())
            .Access.CamelCaseField(Prefix.Underscore)
            .Cascade.SaveUpdate()
            .KeyColumn("EmployeeId");
    }
}

Team:

public sealed class TeamMap : ClassMap<Team>
{
    public TeamMap()
    {
        Id(p => p.Id)
            .Column("TeamId")
            .GeneratedBy.Identity();

        Map(p => p.Name);

        HasManyToMany(p => p.GetEmployees())
            .Access.CamelCaseField(Prefix.Underscore)
            .Table("TeamEmployee")
            .ParentKeyColumn("TeamId")
            .ChildKeyColumn("EmployeeId")
            .LazyLoad()
            .AsSet()
            .Inverse()
            .Cascade.SaveUpdate();
    }
}

How do I fix this bug and what is the problem here?

  • 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-16T16:36:54+00:00Added an answer on May 16, 2026 at 4:36 pm

    You are most probably adding two employees with the same id to the session.

    Find the reason why there are two employees with the same id.

    Are you using detached (e.g. serialized) entities? You most probably have the entity loaded from the database (eg. when loading the team) and another which is detached and gets added. The best you can do is not using the detached instance at all. Only use its id to load the real instance from the database:

    var employee = employeeRepository.GetById(detachedEmployee.id);
    var team = _teamRepository.GetById(teamId);
    if (team != null)
    {
        employee.AddTeam(team);
    }
    _employeeRepository.SaveOrUpdate(employee);
    

    When you want to write the values in the detachedEmployee over the employee in the session, use session.Merge instead of session.Update (or session.SaveOrUpdate):

    var employee = employeeRepository.Merge(detachedEmployee);
    // ...
    

    There are other reasons for this error, but I don’t know what you are doing and how your mapping file looks like.


    Edit

    This should work:

    // put the employee into the session before any query.
    _employeeRepository.SaveOrUpdate(employee);
    
    var team = _teamRepository.GetById(teamId);
    if (team != null)
    {
        employee.AddTeam(team);
    }
    

    Or use merge:

    var team = _teamRepository.GetById(teamId);
    if (team != null)
    {
        employee.AddTeam(team);
    }
    // use merge, because there is already an instance of the
    // employee loaded in the session.
    // note the return value of merge: it returns the instance in the
    // session (but take the value from the given instance)
    employee = _employeeRepository.Merge(employee);
    

    Explanation:

    There could only be one instance of the same database “record” in memory. NH ensures that the same instance is returned by a query as already is in the session cache. If it wouldn’t be like this, there would be more than one value in memory for the same database field. This would be inconsistent.

    Entities are identified by their primary key. So there must be only one instance per primary key value. Instances are put into the session by queries, Save, Update, SaveOrUpdate or Lock.

    You get a problem when an instance is already in the session (eg. by a query) and you try to put another instance (eg. a detached / serialized one) with the same id into the session (eg. using update).

    Solution one puts the instance into the session before any other query. NH will return exactly this instance in all subsequent queries! This is really nice, but you need to call Update before any query to make it work properly.

    Solution two uses Merge. Merge does the following:

    • if the instance is already in the cache, it writes all the properties from the argument into the one in the cache and returns the one in the cache.
    • is the instance not in the cache, it retrieves it from the database (which requires a query). Then it adds the argument into the cache. It returns the also the instance in the cache which is the same as the argument. In contrast to a simple Update, it does not blindly update the values in the database, it retrieves it it first. This allows NH to perform either an insert or update, omit the update at all (because it is unchanged) and some more special cases.

    At the end, Merge does never have a problem with already existing instances. It does not even matter if the instance is already in the database or not. You just need to take into account that the return value is the instance in the session, not the argument.


    Edit: the second Employee instance

    [HttpPost]
    public ActionResult Edit(
        Employee employee, // <<== serialized (detached) instance
        [Optional, DefaultParameterValue(0)] int teamId)
    {
        // ...
    
        // load team and containing Employees into the session
        var team = _teamRepository.GetById(teamId);
    
    
        // ...
    
        // store the detached employee. The employee may already be in the session,
        // loaded by the team query above. The detached employee is another instance
        // of an already loaded one. This is not allowed.
        _employeeRepository.SaveOrUpdate(employee);
    
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Hibernate: different object with the same identifier value was already associated with
Possible Duplicate: Hibernate: different object with the same identifier value was already associated with
Possible Duplicate: Spring + Hibernate : a different object with the same identifier value
I'm trying to do this, but I get the error. a different object with
I get this error: TypeError: object.__init__() takes no parameters when running my code, I
I get this error while accessing a php script: W/System.err: Error reading from ./org/apache/harmony/awt/www/content/text/html.class
I get this error when i try to upload an image: OSError at /upload/
I get this error on my Ice Cream Sandwich (Android 4.0) device and emulator.
I get this error when I run the code below. I have normally used
I get this error when I use simplejson from django.utils in google app engine

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.