title: Understanding the optimistic locking StaleObjectStateException
body: <p>I have been struggling with the <code>StaleObjectStateException</code> for more than a week now and decided to post here a simple app that reproduces the problem.</p>
  
  <p>I understand the <code>org.hibernate.StaleObjectStateException</code> is an <strong>optimistic locking exception</strong>. Furthermore, <strong>optimistic locking relies on the use of a version field</strong> in each of the entity classes.</p>
  
  <p>Now let me explain how I reproduced the above exception: the sample app has an <code>Member</code> entity class which is as follows:</p>
  
  <pre><code>@RooJavaBean
  @RooToString
  @RooJpaEntity
  public class Member {
  
      @OneToOne(cascade=CascadeType.ALL)
      private Address address;
      //id, version fields as well as mutator/accessors are located in a separate Roo ITD/aspect  
  }
  </code></pre>
  
  <p>Here is the <code>Address</code> entity class:</p>
  
  <pre><code>@RooJavaBean
  @RooToString
  @RooJpaEntity
  public class Address {
  
      private String formattedAddress;
      private double lng;
      private double lat;
      //id, version fields as well as mutator/accessors are located in a separate Roo ITD/aspect 
  }
  </code></pre>
  
  <p>Using a <strong>fresh/unmanaged Address instance</strong> and a <strong>managed Member instance</strong>, I am trying to update the member's address in a <code>ServiceImpl</code> class as follows:</p>
  
  <pre><code>@Override
  public void updateMemberAddress(Member member, Address address) {
      long addressId = member.getAddress().getId();
      address.setId(addressId);
      updateAddress(address);
  }
  </code></pre>
  
  <p>Here is the test class:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = "classpath:/META-INF/spring/applicationContext*.xml")
  @TransactionConfiguration(defaultRollback = false)
  public class AddressIntegrationTest {
  
      @Autowired
      private Service service;
  
      @Before
      @Transactional
      public void testInsertOneMember() {
          Member member = new Member();
          Address address = new Address();
          address.setFormattedAddress("Eiffel Tower, Paris");
          address.setLat(48.005);
          address.setLng(3.288);
          member.setAddress(address);
          service.saveMember(member);
      }
  
      @Test
      @Transactional
      public void testUpdateAddress() {
          Member member = service.findAllMembers().get(0);
          Address address = new Address();
          address.setFormattedAddress("Empire State Building, New York");
          address.setLat(200.033);
          address.setLng(36.665);
          service.updateMemberAddress(member, address);
      }
  }
  </code></pre>
  
  <p>Unfortunately, I get the dreaded StaleObjectStateException as follows:</p>
  
  <pre><code>org.springframework.orm.jpa.JpaOptimisticLockingFailureException: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [org.sose.domain.Address#1]; 
  </code></pre>
  
  <p>Anyone wishing to reproduce the problem using the sample github app needs:</p>
  
  <ul>
  <li>Maven</li>
  <li>Git</li>
  <li>JDK 6</li>
  <li>MySQL</li>
  </ul>
  
  <p>They can reproduce the problem by following the steps below:</p>
  
  <ul>
  <li><code>git clone git@github.com:balteo/StaleObjectStateException.git</code></li>
  <li>In mysql create a database schema called sose <code>create database sose;</code></li>
  <li><code>mvn test</code></li>
  <li>and voila: <code>BOOM</code>!</li>
  </ul>
  
  <p>Can anyone please explain to me why this exception is occurring in my case and how to update the address instance without getting this exception?</p>