Results 1 to 4 of 4

Thread: Incrementing a counter when using hibernate

  1. #1
    Join Date
    Feb 2005
    Location
    Norway
    Posts
    18

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Feb 2005
    Location
    Norway
    Posts
    18

    Default

    Quote 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

  4. #4
    Join Date
    Aug 2004
    Location
    Vrhnika, Slovenia
    Posts
    133

    Default

    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.

Similar Threads

  1. Replies: 5
    Last Post: Dec 27th, 2005, 07:00 AM
  2. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  3. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  4. Replies: 9
    Last Post: Sep 25th, 2004, 12:35 PM
  5. Replies: 7
    Last Post: Aug 21st, 2004, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •