
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() {
HibernateTemplate ht = getHibernateTemplate();
Session session = getSession();
Query selectQuery = session.createSQLQuery("SELECT {img.*} FROM image {img} ORDER BY RAND()", "img", RandomImage.class);
selectQuery.setMaxResults(3);
List imgList = null;
try {
imgList = selectQuery.list();
} catch (HibernateException e) {
throw SessionFactoryUtils.convertHibernateAccessException(e);
}
for (RandomImage img : imgList) {
img.counter++;
ht.update(img);
}
return imgList;
}
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