title: Hibernate: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
body: <p>First of all sorry. I know it's have been alot of same topics, but maybe my english bad or maybe I did not find the right answer.</p>
  
  <p>I use Optimistic locking @Version annotation.  I need one for one Row from one table have numerous Rows from different table and I did insert like this:</p>
  
  <p>So it has to look like this:</p>
  
  <blockquote>
    <p>Institution (This is a one of the row of my table(Institution))</p>
  
  <pre><code>       - CreationDate A (this is row from different table(CreationDate) bind with Institution with JoinTable, ManyToMany)
  
         - CreationDate B (this is row from different table(CreationDate) bind with Institution with JoinTable, ManyToMany)
  
         - CreationDate C (this is row from different table(CreationDate) bind with Institution with JoinTable, ManyToMany)
  </code></pre>
  </blockquote>
  
  <p>So, to put this CreationDate in diferent variation i decide to put id manually for particular Institution to which I want to add my CreationDate.</p>
  
  <p>My main method class:</p>
  
  <pre><code>CreationDate crdate = new CreationDate();   
  Institution inst= new Institution();
  Set&lt;Institution&gt; instituset = new HashSet&lt;Institution&gt;();
  
  ***inst.setInstitutionId(2);***
  
  inst.setNameOfInstitution("MyInstitution");
  inst.setTypeName("Education");
  instituset.add(inst);
  </code></pre>
  
  <p>The line which mark with stars i do add in my code when I want to attach to my Institution another row of data <strong>After first insert my Version Row in Database changed to one</strong>.</p>
  
  <p>Here my Institution entity:</p>
  
  <pre><code>@Entity
  @Table(name="INSTITUTION")
  public class Institution implements Serializable{
  
  
      private static final long serialVersionUID = -7636394097858726922L;
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name="INSTITUTION_ID")
      private int institutionId;
  
  
      @Version
      @Column(name="VERSION")
      private int version;
  
  
      @Column(name="NAME_INSTITUTION")
      private String nameOfInstitution;
  
      @Column(name="TYPE_NAME")
      private String typeName;
  
  
      @ManyToMany(mappedBy="institution", fetch = FetchType.LAZY)
      @Cascade(org.hibernate.annotations.CascadeType.ALL)
      private Set&lt;CreationDate&gt; creationDate = new HashSet&lt;CreationDate&gt;();
  
      //getter and setter methods ommited
  
      public String toString() {
          return institutionId + " , " + nameOfInstitution + " , " + typeName 
      }       
  }
  </code></pre>
  
  <p>And my CreatioDate entity:</p>
  
  <pre><code>@Entity
  @Table(name="CREATION_DATE")
  public class CreationDate implements Serializable {
  
  
      private static final long serialVersionUID = 1648102358397071136L;
  
      @Id
      @GeneratedValue(strategy=IDENTITY)
      @Column(name="DATE_ID")
      private int dateId;
  
  
      @Column(name="PARTICULAR_DATE")
      private Date particularDate;
  
      @Version
      @Column(name="VERSION")
      private int version;
  
      @Temporal(TemporalType.DATE)
      @Column(name="CHILD_GO_SCHOOL_DATE")
      private Date childGoSchoolDate;
  
      @Temporal(TemporalType.DATE)
      @Column(name="CHILD_ADMISSION_DATE")
      private Date childAdmissionDate;
  
  
      @ManyToMany(fetch = FetchType.LAZY)
      @Cascade(org.hibernate.annotations.CascadeType.ALL)
      @JoinTable(name="CREATIONDATE_INSTITUTION", 
                                      joinColumns=@JoinColumn(name="DATE_ID"), 
                      inverseJoinColumns=@JoinColumn(name="INSTITUTION_ID"))
      private Set&lt;Institution&gt; institution = new HashSet&lt;Institution&gt;();
  
      @OneToMany(fetch=FetchType.EAGER, orphanRemoval=true)
      @Cascade(org.hibernate.annotations.CascadeType.ALL)
      @JoinTable(name="SRC_DATE", joinColumns=@JoinColumn(name="DATE_ID"),
                      inverseJoinColumns=@JoinColumn(name="SRC_ID"))
      private List&lt;ScheduleRotationChild&gt; scheduleRotationChild = new ArrayList&lt;ScheduleRotationChild&gt;();
  
  
          //getter and setter methods ommited
  
  
      public String toString() {
  
          return  dateId + " , " 
              + particularDate + " , " 
              + childGoSchoolDate + " , " 
              + childAdmissionDate + "  " + scheduleRotationChild ;
  
      }
  }
  </code></pre>
  
  <p>My dao:</p>
  
  <pre><code>public CreationDate insertData(CreationDate creationdate) {
          sessionFactory.getCurrentSession().saveOrUpdate(creationdate);
          log.info(creationdate.getDateId());
          return creationdate;
  
      }
  </code></pre>
  
  <p>So the exception occurs when I try to insert my data to my Institution the secont time.</p>
  
  <pre><code> Hibernate: insert into CREATION_DATE (CHILD_ADMISSION_DATE,
   CHILD_GO_SCHOOL_DATE, PARTICULAR_DATE, VERSION) values (?, ?, ?, ?)
  
   Hibernate: insert into SCHEDULE_ROTATION_CHILD (CHILD_ADMITTED,
   CHILD_GO_SCHOOL, CHILD_UNDER_3_YEARSOLD, CHILD_UPPER_3_YEARSOLD,
   DAY_SCHEDULE, NUMBER_OF_CHILD, ROTATION, VERSION, WORK_SCHEDULE)
   values (?, ?, ?, ?, ?, ?, ?, ?, ?)
  
   Hibernate: update INSTITUTION set
   NAME_INSTITUTION=?, TYPE_NAME=?, VERSION=? where INSTITUTION_ID=? and
   VERSION=? Exception in thread "main"
  
  org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException:
       Object of class [edu.demidov.dom.Institution] with identifier [2]:
       optimistic locking failed; nested exception is
       org.hibernate.StaleObjectStateException: Row was updated or deleted by
      another transaction (or unsaved-value mapping was incorrect):
       [edu.demidov.dom.Institution#2]
  </code></pre>