title: Hibernate @Version annotation
body: <p>What is the relation between hibernate @version and ManyToOne Mapping.</p>
  
  <p>Assume that i am having two tables Department and Employee. Here is Deparment is the master table
  and Employee in the detail table. In the Employee table, departmentID is reference as foreign key.</p>
  
  <p>Here is my classes</p>
  
  <pre><code>Public class Department {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private long ID;
      @Version
      private Long version;
  
      //Getters and Setters
  
  }
  
  public class Employee {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private long ID;
      @Version
      private Long version;
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "departmentID" )
      private Department department;
  
  }
  </code></pre>
  
  <p>And also, Spring handles the session. So assume that, in one page, particular department is fetched and stored
  in the HTTP session.</p>
  
  <p>Now in another page, i am trying to do the following</p>
  
  <pre><code>Employee emp = new Employee();
  emp.setName('Test')
  emp.setDepartment(dept) // already stored in the HTTP session variable
  service.save(emp)
  </code></pre>
  
  <p>Now i am getting the following exception</p>
  
  <pre><code>org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: 
  </code></pre>
  
  <p>And just it make one change as follow and there is errror</p>
  
  <pre><code>Employee emp = new Employee();
  emp.setName('Test')
  dept.setVersion(0);
  emp.setDepartment(dept) // already stored in the HTTP session variable
  service.save(emp)
  </code></pre>
  
  <p>My Spring config</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/tx
                 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&gt;
  
  &lt;bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Container Configuration: The IOC container configuration xml file is 
      shown below,The container has the &lt;context:component-scan&gt; element and &lt;context:annotation-config/&gt; 
      &lt;context:annotation-config/&gt; used to intimate the beans of this IOC container 
      are annotation supported. By pass the base path of the beans as the value 
      of the base-package attribute of context:component-scan element, we can detect 
      the beans and registering their bean definitions automatically without lots 
      of overhead. The value of base-package attribute is fully qualified package 
      name of the bean classes. We can pass more than one package names by comma 
      separated --&gt;
  
  &lt;context:annotation-config /&gt;
  &lt;context:component-scan base-package="com.product.business" /&gt;
  
  &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  &lt;!-- This will ensure that hibernate or jpa exceptions are automatically 
      translated into Spring's generic DataAccessException hierarchy for those 
      classes annotated with Repository --&gt;
  
  &lt;bean
      class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /&gt;
  
  &lt;bean id="CRUDService" class="com.product.business.service.CRUDServiceImpl" /&gt;
  &lt;bean id="AuthService" class="com.product.business.service.AuthServiceImpl" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>Service Implementation</p>
  
  <pre><code>package com.product.business.service;
  
  import java.io.Serializable;
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.dao.DataAccessException;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import com.product.business.dao.CRUDDao;
  
  @Service
  public class CRUDServiceImpl implements CRUDService {
  
      @Autowired
      private CRUDDao CRUDDao;
  
      @Transactional(readOnly = true)
      public &lt;T&gt; List&lt;T&gt; getAll(Class&lt;T&gt; klass) {
          return CRUDDao.getAll(klass);
      }
  
      @Transactional
      public &lt;T&gt; void Save(T klass) throws DataAccessException {
          CRUDDao.Save(klass);
      }
  
      @Transactional
      public &lt;T&gt; void delete(T klass) throws DataAccessException {
          CRUDDao.delete(klass);
      }
  
      @Transactional
      public &lt;T&gt; T GetUniqueEntityByNamedQuery(String query, Object... params) {
          return CRUDDao.GetUniqueEntityByNamedQuery(query, params);
      }
  
      @Transactional
      public &lt;T&gt; List&lt;T&gt; GetListByNamedQuery(String query, Object... params) {
          return CRUDDao.GetListByNamedQuery(query, params);
      }
  
      @Override
      @Transactional(readOnly = true)
      public &lt;T&gt; Long getQueryCount(String query, Object... params) {
          return CRUDDao.getQueryCount(query, params);
      }
  
      @Override
      @Transactional(readOnly = true)
      public &lt;T&gt; T findByPrimaryKey(Class&lt;T&gt; klass, Serializable id) {
           return CRUDDao.findByPrimaryKey(klass, id);
      }
  
  }
  </code></pre>