title: Updating counters through Hibernate
body: <p>This is an extremely common situation, so I'm expecting a good solution.  Basically we need to update counters in our tables.  As an example a web page visit:</p>
  
  <pre><code>Web_Page
  --------
  Id
  Url
  Visit_Count
  </code></pre>
  
  <p>So in hibernate, we might have this code:</p>
  
  <pre><code>webPage.setVisitCount(webPage.getVisitCount()+1);
  </code></pre>
  
  <p>The problem there is reads in mysql by default don't pay attention to transactions.  So a highly trafficked webpage will have inaccurate counts.</p>
  
  <p>The way I'm used to doing this type of thing is simply call:</p>
  
  <pre><code>update Web_Page set Visit_Count=Visit_Count+1 where Id=12345;
  </code></pre>
  
  <p>I guess my question is, how do I do that in Hibernate?  And secondly, how can I do an update like this in Hibernate which is a bit more complex?</p>
  
  <pre><code>update Web_Page wp set wp.Visit_Count=(select stats.Visits from Statistics stats where stats.Web_Page_Id=wp.Id) + 1 where Id=12345;
  </code></pre>