Hi All,
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. I do implement equals and hashcode on the model objects which I try to persist. If I try to debug, the equals() isn't invoked at all. I am really not sure what's going on here.
I am using Spring's OSIVF to open and close sessions and AOP to demarcate transactions and am using EhCache as my 2nd level cache.
hibernate.cfg.xmlCode:
<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>
<!-- 2nd level cache with EHCache -->
<property name="hibernate.cache.region.factory_class"> net.sf.ehcache.hibernate.EhCacheRegionFactory</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</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
Code:
<hibernate-mapping>
<class name="com.spring.model.Employee" table="employee">
<cache usage="read-only" />
<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
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
@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;
//getters/setters
}
public class Identity {
protected String firstname;
protected String lastname;
protected String email;
protected String telephone;
protected String birthday;
//getters/setters
My DAO classesCode:
public class EmployeeService implements EmployeeBaseService{
@Override
public Employee getEmployeeById(long empId) {
// TODO Auto-generated method stub
return empDaoImpl.getEmployeeById(empId);
}
@Override
public long saveEmployee(Employee employee) throws Exception {
// TODO Auto-generated method stub
return empDaoImpl.saveEmployee(employee);
}
}
public class EmployeeDaoImpl extends BaseHibernateDaoSupport{
public Employee getEmployeeById(long empId) {
// TODO Auto-generated method stub
return (Employee) getSessionFactory().getCurrentSession().load(Employee.class,empId);
}
public long saveEmployee(Employee employee) throws Exception {
// TODO Auto-generated method stub
if(employee.getEmpId() == 0){
//new employee
return (Long)getSessionFactory().getCurrentSession().save(employee);
}else{
Employee empInSession = getEmployeeById(employee.getEmpId());
//employee.setEmpId(empInSession.getEmpId());
getSessionFactory().getCurrentSession().merge(employee);
System.out.println(employee.getEmpId());
return employee.getEmpId
The tx. demarcation works correctly , as the new object is persisted successfully, however, after saving the get returns the old object. If I use saveorupdate() instead of merge, it saves a new object,detaches the old object and attaches the new obj. to the session. Can somebody please help me on this? Thanks a lot.