title: @Transactionnal + Hibernatetemplate Without saving dirty objects
body: <p>I already search in forums, and i tried many things without succes.</p>
  
  <p>I want at the end on a transactional method, and only at the end, to save only hibernate object for whom i will use <strong>explicitly</strong> the Hibernate <strong>getHibernateTemplate</strong>().saveOrUpdate() method, and <strong>NOT dirty</strong> ones, that i only modify using setters.</p>
  
  <p>My need will be clearer after reading my service layer code:</p>
  
  <p>For simplicity purpose, i will <strong>summarize</strong> my code.</p>
  
  <h2>Service layer:</h2>
  
  <pre><code>@Service("cartService")
  public class CartServiceImpl extends AbstractServiceImpl&lt;Cart&gt; implements
          CartService {
  
     @Autowired 
     private CartDao cartDao;
  
     @Transactional(readOnly = false)
     public Cart updateCart1(){
  
              Cart cartA = cartDao.findById(1);
              cartA.setTotal=(5);
  
              Cart cartB = cartDao.findById(1);
              cartB.setTotal=(5);     
              CartDao.Update(cartB);
     }
  }
  </code></pre>
  
  <p>So, what i need, is at the end of the method updateCart1() is to <strong>save</strong> the change of <strong>cartB</strong>, BUT <strong>Not</strong> to save the change of <strong>cartA</strong>.</p>
  
  <h2>Dao layer:</h2>
  
  <p>NB: cartDao's methods (findById, saveOrUpdate) <strong>call the HibernateTemplate</strong> one. </p>
  
  <pre><code>public T update(T entite) {
      getHibernateTemplate().saveOrUpdate(entite);
      return entite;
  }
  
  public T findById(Serializable id) {
      return getHibernateTemplate().get(this.clazz, id);
  }
  </code></pre>
  
  <p>I didn't want that Dao's method act on database outside a transactionnal service layer method. And i wanted them to be executed in database at the <strong>end of a transaction</strong> defined on the service layer. Thus, in <strong>hibernate spring</strong> configuration file: </p>
  
  <pre><code>&lt;prop key="connection.autocommit"&gt;false&lt;/prop&gt; 
  </code></pre>
  
  <p>Note that i am using <strong>OpenSessionInViewFilter</strong> for Lazyu loading purpose. So in my XML file:</p>
  
  <pre><code>&lt;filter&gt;
    &lt;filter-name&gt;OpenSessionInViewFilter&lt;/filter-name&gt;
    &lt;filter-class&gt;org.springframework.orm.hibernate3.support.OpenSessionInViewFilter&lt;/filter-class&gt;
    &lt;init-param&gt;
       &lt;param-name&gt;singleSession&lt;/param-name&gt;
       &lt;param-value&gt;false&lt;/param-value&gt;
    &lt;/init-param&gt; 
  &lt;/filter&gt;
  </code></pre>
  
  <p>NB: These cart <strong>cartDao's</strong> methods are working <strong>fine</strong> in a normal scenario. But <strong>not</strong> in the one i'm describing. And "<strong>@Transactional</strong>" is correctly configured and work fine two. </p>
  
  <p><strong>Problem</strong>: With this code, i get the two objects (cartA and cartB) changed in database, while i dont want to modify cartA, for whom i didn't call explicitly the HibernateTemplate's saveOrUpdate method.</p>
  
  <p>After searching in <strong>forums</strong>, i tried to make the hibernate FLUSH mode to <strong>FLUSH_NEVER</strong>. But at the execution of  CartDao.Update(cartB) i get this exception:</p>
  
  <pre><code>org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
  </code></pre>
  
  <p><strong>Please, can you help me?</strong> </p>