Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate saves a new object with every merge
    primarykey
    data
    text
    <p>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.</p> <p>Please find below my code:</p> <p><strong>My hibernate.cfg.xml</strong></p> <pre><code>&lt;hibernate-configuration&gt; &lt;session-factory&gt; &lt;property name="dialect"&gt;org.hibernate.dialect.Oracle10gDialect &lt;/property&gt; &lt;!-- this will show us all sql statements --&gt; &lt;property name="hibernate.show_sql"&gt; true &lt;/property&gt; &lt;property name="connection.pool_size"&gt;1&lt;/property&gt; &lt;!-- &lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt;--&gt; &lt;!-- mapping files --&gt; &lt;mapping resource="com/hibernate/hbm/employee.hbm.xml"&gt;&lt;/mapping&gt; &lt;/session-factory&gt; &lt;/hibernate-configuration&gt; </code></pre> <p>My <strong>employee.hbm.xml</strong></p> <pre><code>&lt;hibernate-mapping default-lazy="true"&gt; &lt;class name="com.spring.model.Employee" table="employee"&gt; &lt;id name="empId" type="long" column="empId" unsaved-value="null"&gt; &lt;generator class="sequence"&gt; &lt;param name="sequence"&gt;hibernate_sequence&lt;/param&gt; &lt;/generator&gt; &lt;/id&gt; &lt;version name="version" column="version" unsaved-value="null" type="long" /&gt; &lt;component name="identity" class="com.spring.model.Identity"&gt; &lt;property name="firstname" column="firstname" not-null="true" /&gt; &lt;property name="lastname" column="lastname" not-null="true" /&gt; &lt;property name="email" column="emailid" not-null="true" /&gt; &lt;/component&gt; &lt;!-- &lt;property name="birthday" column="birthday"/&gt; --&gt; &lt;property name="fileDataBytes" column="filedata" /&gt; &lt;property name="fileName" column="fileName" /&gt; &lt;property name="fileContentType" column="fileContentType" /&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>My <strong>model classes</strong></p> <pre><code> 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 &gt;&gt;&gt; 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 </code></pre> <p>My <strong>employeeDAOImpl.java's save</strong> method</p> <pre><code>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(); } } </code></pre> <p>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??</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload