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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:48:28+00:00 2026-06-08T12:48:28+00:00

Having a NHibernate entity: public class Employee { public virtual int Id { get;

  • 0

Having a NHibernate entity:

public class Employee
{
    public virtual int Id { get; set; }
    public virtual int Idc { get; set; }
    public virtual int Ide { get; set; }
    .
    . other properties
    .
}

with Id mapped as:

<id name="Id" unsaved-value="0">
    <generator class="sequence">
        <param name="sequence">employee_id_seq</param>
    </generator>
</id>

If I populate all properties of a new Employee except Id and then call session.Merge() it just creates another row with all properties identical to the original row except Id instead of merging with the existing employee.

Is it possible to do instead an update at database level on the row corresponding to those properties?
Idc + Ide together are unique so it should be possible to identify the row to be merged.

Thanks for your help!

  • 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-06-08T12:48:29+00:00Added an answer on June 8, 2026 at 12:48 pm

    NHibernate session maintains an identity map for all currently loaded (persisted) objects. Identity map uses the entity id as the key. In your case, your Id is a simple int value and since it’s equal to 0 (by default) for newly created object, session.Merge() can’t find the matching persistent entity in session. Instead, a new row is added to database.

    Read Ayende’s post about cross-session operations for more in depth explanation.

    In other cases, where you want to have a different notion of entity equality, you would override Equals() and GetHashCode() methods of your entity – i.e. to compare all properties, but I’m afraid, it won’t help you in this case.

    Just for the reference, here are some links about Equals() and GetHashCode() usage:
    Is there a sample why Equals/GetHashCode should be overwritten in NHibernate?
    NHibernate: Reasons for overriding Equals and GetHashCode

    Edit

    There are two options to update the row at database level:

    1. You could do NHibernate update query, something along these lines:

      Employee emp; // = your new employee instance
      session.CreateQuery(
                 "update Employee set Property1 = :property1, ... " +
                 "where Idc = :idc, Ide = :ide")
             .SetParameter("idc", emp.Idc)
             .SetParameter("ide", emp.Ide)
             .SetParameter("property1", emp.Property1)
             // other properties
             .ExecuteUpdate();
      

      Also, you could use native SQL to do this: session.CreateSQLQuery("...").ExecuteUpdate();

    2. Or you could load the entity first, update its properties and then save it:

      Employee emp; // = your new employee instance
      Employee oldEmployee = session.Query<Employee>()
          .Where(x => x.Idc == emp.Idc)
          .Where(x => x.Ide == emp.Ide)
          .Single();
      oldEmployee.Property1 = emp.Property1;
      oldEmployee.Property2 = emp.Property2;
      // other properties
      session.SaveOrUpdate(oldEmployee);
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following 2 entities: @Entity(name = Employee) @Table(name = EMPLOYEE) public class
A typical JPA entity looks like: @Entity public class Person { @Id private int
Having tried several solutions to get NHibernate to delete orphan records. Given the following
I'm having a problem with Fluent NHibernate mappings, I think, and can't quite get
Is having an entity named Order an issue with nhibernate? Update I ask because,
I have an Entity with a Collection, which holds other entities having more References
I am having trouble setting up a Fluent NHibernate HasMany collection. I have set
Consider an entity like so public class SomeEntity { @ManyToOne @JoinColumn(name = company_fk) private
I'm having some problems using cascade operations on hibernate. I have one entity (TripleDBmodel),
I'm using ASP.NET MVC + NHibernate + Fluent NHibernate and having a problem with

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.