title: spring hibernate optimistic locking issue
body: <p>This is my Repository layer:</p>
  
  <pre><code>class RepositoryImpl implements Repository{ 
      @Override
      public Serializable saveOrUpdate(Object obj) {      
          return getSession().save(obj);
      }
  
      @Override
      public Object get(Class refClass, Serializable key)
      {
              return getSession().get(refClass, key);
      }
  }
  </code></pre>
  
  <p>This is my Service layer:</p>
  
  <pre><code>class ServiceImpl implements Service{
  
      @Autowired
      Repository repository;
  
      @Override
      @Transactional
      public Object findUserById(Serializable key) {
          // TODO Auto-generated method stub
          return repository.get(User.class,key);
      }
  
      @Override
      @Transactional
      public Serializable saveUser(Object o) {
          // TODO Auto-generated method stub
          return repository.saveOrUpdate(o);
      }   
  }
  </code></pre>
  
  <p>Here I am accessing and updating objects asynchronously:</p>
  
  <pre><code>class SomeClass{
      @Autowired
      Service service;    
      public void asynchronousSaveOrUpdate(){
          User u = service.findUserbyId(1);
          service.saveUser(1);
      }
      public void asynchronousfindUsersById(){
          service.findUserbyId(1);
      }
  }
  </code></pre>
  
  <p>I got this exception:</p>
  
  <pre><code>[1236]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.deadline.core.model.User#1236]
          at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:672)
          at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:793)
          at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:664)
          at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
          at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
          at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
          at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
  </code></pre>
  
  <p>Note: I don't want <code>Transactional</code> for ServiceImpl.findUserById() method.</p>
  
  <p>Doing <code>@Transactional(readOnly =true)</code> or <code>@Transactional(propagation =Propagation.REQUIRES_NEW)</code> will solve this problem?</p>
  
  <p>By calling <code>session.merge()</code> this issue will be solved, however I cannot use it.</p>