-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate Spring MVC application merge() saves a new object
PostPosted: Wed May 23, 2012 5:34 am 
Newbie

Joined: Sat Sep 13, 2008 9:45 am
Posts: 4
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.xml
Code:
<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 classes

Code:
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.

_________________
To living life on the edge!!! I blog my experiences @http://anirbanchowdhury.wordpress.com/


Top
 Profile  
 
 Post subject: Re: Hibernate Spring MVC application merge() saves a new object
PostPosted: Mon May 28, 2012 3:04 am 
Newbie

Joined: Sat Sep 13, 2008 9:45 am
Posts: 4
Well, this was a Spring configuration issue, got resolved by adding @SessionAttributes("organization") to the Controller.

_________________
To living life on the edge!!! I blog my experiences @http://anirbanchowdhury.wordpress.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.