Announcement Announcement Module
No announcement yet.
HibernateTemplate.load increments version? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • HibernateTemplate.load increments version?

    I'm working with the hibernate template using Oracle 9i. I have a dao object with code that looks like this:
    MyObject result = null;
    	HibernateTemplate template = new HibernateTemplate(getSessionFactory());
    	result = (MyObject) template.load(MyObject.class, id, LockMode.READ);
    catch (Throwable t)
    return result;
    For some reason, every time the object gets loaded, the version number used for optimistic locking (set using the "version" setting in the hibernate mapping) is ALWAYS incremented. This shouldn't be happening and I can't figure out why it would be happening. The unit test I run with this checks this code only, so there's nothing else happening. If you replace the two most important lines with the following, the version number doesn't get incremented, which is what should be happening:
    session = getSessionFactory().openSession();
    result = (MyObject) session.load(MyObject.class, id, LockMode.READ);
    So using the standard loading does not increment the version number. I am certain that I don't want version numbers incremented on every load. What could be causing this? Is it somehow related to the behind-the-scenes work spring does with the hibernate template to bind to the current thread?

  • #2
    Answered my own question

    Here's the deal:
    1) The dao call was wrapped in a facade class that (mistakenly) created a transaction.
    2) Within the dao call, I manually set the flush mode to FLUSH_NEVER:
    It appears that both need to happen to keep version from incrementing. You have to explicitly turn off flushing or else it increments, and even with that, if you wrap it in a transaction, the fact that a transaction is in process prevents the template code from detecting the FLUSH_NEVER.

    At least that's what I found.


    • #3
      I guess it would also work with a read-only transaction (which implicitly sets the underlying Hibernate Session to FLUSH_NEVER): Try adding ",readOnly" to your transaction attribute (in the declarative case), respectively to call setReadOnly(true) on your TransactionTemplate.

      However, I don't understand why you experience that version increase in the first place...