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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:46:58+00:00 2026-06-04T18:46:58+00:00

I am having a peculiar problem. Every time,I call merge, on the session, Hibernate

  • 0

I am having a peculiar problem. Every time,I call merge, on the session, Hibernate persists a brand new object.
I am using Hibernate 3.6 in a Spring MVC application.

Please find below my code:

My hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect </property>





        <!-- this will show us all sql statements -->
        <property name="hibernate.show_sql"> true   </property>
        <property name="connection.pool_size">1</property>


        <!--  <property name="hbm2ddl.auto">create</property>-->

        <!-- mapping files -->


        <mapping resource="com/hibernate/hbm/employee.hbm.xml"></mapping>
        </session-factory>
</hibernate-configuration>

My employee.hbm.xml

<hibernate-mapping default-lazy="true">
    <class name="com.spring.model.Employee" table="employee">
        <id name="empId" type="long" column="empId" unsaved-value="null">
            <generator class="sequence">
                <param name="sequence">hibernate_sequence</param>
            </generator>
        </id>
        <version name="version" column="version" unsaved-value="null"
            type="long" />


        <component name="identity" class="com.spring.model.Identity">
            <property name="firstname" column="firstname" not-null="true" />
            <property name="lastname" column="lastname" not-null="true" />
            <property name="email" column="emailid" not-null="true" />
        </component>


        <!--    <property name="birthday" column="birthday"/>       -->
        <property name="fileDataBytes" column="filedata" />

        <property name="fileName" column="fileName" />
        <property name="fileContentType" column="fileContentType" />

    </class>
</hibernate-mapping>

My model classes

  public class Employee  extends BaseModel{
   private CommonsMultipartFile fileData;


private byte[] fileDataBytes;
private String fileName;
private String fileContentType;

private Identity identity;

private long empId;
      //getters,setters /equals() on empId field 

       @Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + (int) (empId ^ (empId >>> 32));
    return result;
}


@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Employee other = (Employee) obj;
    if (empId != other.empId)
        return false;
    return true;
}

       public class BaseModel implements Serializable{
       private Long version;

       //gettes,setters

      public class Identity {
       protected String firstname;   
protected String lastname;   
  protected String email;    
        //getters.setters

My employeeDAOImpl.java’s save method

public long saveEmployee(Employee employee) throws Exception {
        public Employee getEmployeeById(long empId) {
        // TODO Auto-generated method stub
        return (Employee) getSessionFactory().getCurrentSession().load(Employee.class,empId);
                       }
    }
        // TODO Auto-generated method stub
        if(employee.getEmpId() == 0){
             return  (Long)getSessionFactory().getCurrentSession().save(employee);
        }else{
            Employee empInSession = getEmployeeById(employee.getEmpId());
            getSessionFactory().getCurrentSession().merge(employee);
            return  employee.getEmpId();

        }
    }

Note : i already had loaded the object in the GET cycle , but to ensure that the object is loaded in the cache, I am still loading it before the call to merge().Ideally, the merge shouldn’t be required at all,as the object becomes persistent. Why the hell does this happen?
Hibernate saves a new object with the changed properties and saves it. Shouldn’t it check via the empId field which is in the equals check??

  • 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-04T18:47:00+00:00Added an answer on June 4, 2026 at 6:47 pm

    Well, this seemed to be a Spring issue. Got resolved by adding @SessionAttributes to my Controller class.Seems innocuous but actually was the root of the problem. Marten on the Spring forum helps me here

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

Sidebar

Related Questions

I'm having quite a peculiar problem with PhoneGap and JQuery Mobile, using the latest
I am having a peculiar problem with getting an integer from an ajax response.
I'm having the most peculiar problem, and I was hoping I someone could point
Having this problem with my android app I struggled with this for some time
I am having a peculiar problem with the order in which TextBox controls are
I am having a peculiar problem with the order in which FlowLayoutPanels are added
I'm having a peculiar problem in a WPF-app I've written. It has a single
Having hard time figuring out how to take the returned orm object and converting
Having trouble using libstatgrab -- I receive the following error at compile time: libstatgrabTest.cpp:16:
Having a slight problem with the following block of code: newusr = c.readLine(New user?

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.