Announcement Announcement Module
Collapse
No announcement yet.
Incrementing a counter when using hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Incrementing a counter when using hibernate

    Hi all!

    On my site I would like to have a few randomly selected images shown everytime the user refreshes the page. I'm using Spring 1.1.5, Hibernate 2.1.8 with the OpenSessionInViewFilter


    Images are stored on disk and filename, size etc are stored in a mysql table.

    To select images:
    SELECT id, file, counter from image_table ORDER BY RAND() LIMIT 3;

    And then to increment the counter for every image I would:
    UPDATE image_table SET counter = counter + 1 WHERE id IN (selected id's);


    I have managed to create the select statement using session.createSQLQuery, but I haven't found a way to use the Query interface with an update statement.


    Any help would be much appreciated.


    Trond

  • #2
    AFAIK Hibernate2 does not support UPDATE queries. You may either switch to Hibernate3, use plain JDBC or provide a mapping to your Table / Class.

    HTH

    Comment


    • #3
      Originally posted by irbouho
      AFAIK Hibernate2 does not support UPDATE queries. You may either switch to Hibernate3, use plain JDBC or provide a mapping to your Table / Class.
      I have a mapping for my Class. And my DAO extends the HibernateDAOSupport class. I'm using a HibernateTransactionManager and these transactionAttributes:

      Code:
      <property name="transactionAttributes">
          <props>
              <prop key="getRandomImages">PROPAGATION_REQUIRED</prop>
              <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
          </props>
      </property>
      And getRandomImages is implemented like this:
      Code:
      public List getRandomImages&#40;&#41; &#123;
          HibernateTemplate ht = getHibernateTemplate&#40;&#41;;
      
          Session session = getSession&#40;&#41;;
          Query selectQuery = session.createSQLQuery&#40;"SELECT &#123;img.*&#125; FROM image &#123;img&#125; ORDER BY RAND&#40;&#41;", "img", RandomImage.class&#41;;
          selectQuery.setMaxResults&#40;3&#41;;
      
          List imgList = null;
          try &#123;
              imgList = selectQuery.list&#40;&#41;;
          &#125; catch &#40;HibernateException e&#41; &#123;
              throw SessionFactoryUtils.convertHibernateAccessException&#40;e&#41;;
          &#125;
      
          for &#40;RandomImage img &#58; imgList&#41; &#123;
              img.counter++;
              ht.update&#40;img&#41;;
          &#125;
      
          return imgList;
      &#125;
      I'm no expert at Hibernate (obviously) nor transactions...
      Would this be enough to ensure that the counters are incremented properly?
      Only one thread will access the method at the time?


      Trond

      Comment


      • #4
        Use native SQL with Hibernate (in order to use rand function). Then simply increment counter property and let Hibernate do the update (while still in open session - no need for explicit call to update - dirty check will pick it up) or call session.update explicitly.

        Comment

        Working...
        X