Results 1 to 3 of 3

Thread: HibernateTemplate.load increments version?

  1. #1
    Join Date
    Aug 2004
    Location
    San Francisco, CA
    Posts
    4

    Default 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:
    Code:
    MyObject result = null;
    try
    {
    	HibernateTemplate template = new HibernateTemplate(getSessionFactory());
    	result = (MyObject) template.load(MyObject.class, id, LockMode.READ);
    }
    catch (Throwable t)
    {
    	processThrowable(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:
    Code:
    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. #2
    Join Date
    Aug 2004
    Location
    San Francisco, CA
    Posts
    4

    Default 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:
    Code:
    template.setFlushMode(HibernateTemplate.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. #3
    Join Date
    Aug 2004
    Location
    Linz, Austria
    Posts
    391

    Default

    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...

    Juergen

Similar Threads

  1. Replies: 2
    Last Post: Sep 12th, 2007, 12:23 PM
  2. SnpePetClinic and last version of rcp
    By panoramix in forum Swing
    Replies: 4
    Last Post: Aug 24th, 2006, 02:23 AM
  3. Replies: 1
    Last Post: Jul 21st, 2005, 10:28 AM
  4. hibernate version
    By Gary in forum Data
    Replies: 1
    Last Post: Feb 1st, 2005, 04:13 PM
  5. New version of springworkflow
    By rharing in forum Web
    Replies: 2
    Last Post: Jan 10th, 2005, 05:29 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
  •