title: Hibernate 3 : optimistic locking unit test
body: <p>I'm trying to test my optimistic locking implementation of my application.  However the result is not what I expect.  The steps I take to test are the following</p>
  
  <ul>
  <li>load an entity from the database</li>
  <li>set the version attribute to one less than present in the database</li>
  <li>change another attribute thats just a string to something else</li>
  <li>save the entity</li>
  </ul>
  
  <p>I expected a staleException now, however the entity just gets saved and the version increases to the next in line.</p>
  
  <p>Here is a small extract of my save code</p>
  
  <pre><code>   public &lt;T extends DomainObject&gt; T save(T objectToSave) {
          Session currentSession = null;
          try {
              currentSession = sessionFactory.getCurrentSession();
              currentSession.save(objectToSave);
              return objectToSave;
          } catch (Exception ex) {
              logger.error("save error",ex);
          }
          return null;
      }
  </code></pre>
  
  <p>I load objects by id with named queries thru my entire application with following code</p>
  
  <pre><code>@SuppressWarnings("unchecked")
  public &lt;T extends Object&gt; List&lt;T&gt; query(Class&lt;T&gt; returnClass, String query, List&lt;String&gt; namedParams, List&lt;? extends Object&gt; params, Integer limit) {
      Session currentSession = null;
      try {
          currentSession = sessionFactory.getCurrentSession();
          Query namedQuery = currentSession.getNamedQuery(query);
          if(limit != null){
              namedQuery.setMaxResults(limit);
          }
          namedQuery.setCacheable(true);
          if (namedParams != null &amp;&amp; namedParams.size() &gt; 0) {
              addParams(namedQuery, namedParams, (List&lt;Object&gt;) params);
          }
          return namedQuery.list();
      } catch (Exception ex) {
          logger.error("query error",ex);
      }
      return null;
  }
  </code></pre>
  
  <p>And this is the configuration of my sessionfactory</p>
  
  <pre><code>&lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="entityInterceptor" ref="auditInterceptor" /&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;value&gt;
              hibernate.dialect=${hibernate.dialect}
              hibernate.show_sql=${hibernate.show_sql}
              hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
              hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.EhCacheRegionFactory
              hibernate.cache.use_query_cache=true
              hibernate.cache.use_second_level_cache=true
              hibernate.cache.provider_configuration_file_resource_path=ehcache.xml
              hibernate.generate_statistics=true
              hbm2ddl.auto=${hbm2ddl.auto}
              hibernate.c3p0.min_size=${hibernate.c3p0.min_size}
              hibernate.c3p0.max_size=${hibernate.c3p0.max_size}
              hibernate.c3p0.timeout=${hibernate.c3p0.timeout}
              hibernate.c3p0.max_statements=${hibernate.c3p0.max_statements}
              hibernate.c3p0.idle_test_period=${hibernate.c3p0.idle_test_period}
          &lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="schemaUpdate"&gt;
          &lt;value&gt;true&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="annotatedClasses"&gt;
          &lt;list&gt;
              &lt;value&gt;com.mbalogos.mba.domain.site.Site&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;          
  </code></pre>
  
  <p>Is there anything I missed on the configuration of the sessionFactory or do I test it completly wrong and should I test it in a different way ?</p>
  
  <p>Thanks in advance</p>