These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Why automatic versioning not work?
PostPosted: Sat May 15, 2004 5:37 am 
Newbie

Joined: Sat May 15, 2004 5:24 am
Posts: 2
Hi.
I have a problem about automatic versioning with hibernate(2.1.3).
When i execute ProductDAO.updateProduct() method, automatic versioning works well.
But in ProductDAO.updateTest() method, automatic version can't work.
Why?
Sorry for my poor english.
Thanx.

Code:
public class Product {
   private Integer   id;
   private String     name;
   private int          version;

   /**
    * @hibernate.id
    *      column="PRODUCT_ID"
    *      generator-class="native"
    *      unsaved-value="null"
    */
   public Integer getId() {
      return id;
   }

   /**
    * @hibernate.property
    *      column="NAME"
    *      not-null="true"
    *      unique="true"
    *      length="64"
    */
   public String getName() {
      return name;
   }

   /**
    * @hibernate.version
    *      column="VERSION"
    *      type="integer"
    *      unsaved-value="0"
    */
   public int getVersion() {
      return version;
   }

   // setter methods below
   ......
}

public class ProductDAO extends HibernateDAOSupport {
       ......
       public void updateProduct(Product product)
         throws DAOException {
           // automatic versioning works well
           getHibernateTemplate().update(product);
       }

   public void updateTest(Integer id, String name, int oldVersion)
   throws DAOException {

      List products =
         getHibernateTemplate().find("from Product p order by p.id");

      for (Iterator it = products.iterator(); it.hasNext(); ) {
         Product p = (Product)it.next();
         if (p.getId().equals(id)) {
            p.setName(name);
            System.out.println(p.getVersion()); // nowVersion = 2
            p.setVersion(oldVersion); // oldVersion = -1
            // update will success.
            // why StableObjectStateException not occurs?
            updateProduct(p);
            break;
         }
      }
   }
   ......
}


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 15, 2004 5:47 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
1. There is no Hibernate code in your posting. We don't know what updateProduct() does.

2. You shouldn't set the version yourself. It's _automatic_, that means you are safe from concurrent changes automatically.

3. You have the wrong idea about update(). Please reread some Hibernate tutorials and check the API documentation, update() doesn't UPDATE neccessarily, it's used to reattach a detached instance to a new Session.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Why automatic versioning not work?
PostPosted: Sat May 15, 2004 5:59 am 
Newbie

Joined: Sat May 15, 2004 5:24 am
Posts: 2
Hi, christian.

As you say, i dont understand about UPDATE.
I will read Reference & API document.
Thanx for good advice.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.