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