J'essaye d'utiliser le
versionnage automatique en mode détaché tel que décrit dans le chapitre 11.3.3.
voir
http://www.hibernate.org/hib_docs/v3/re ... c-detached
http://www.hibernate.org/hib_docs/v3/re ... c-detached
Voici mon problème :
Si je crée une personne, alors ma version est à 0
lors d'une sauvegarde je passe à 1
si je sauve l'objet en version 0, je m'attends à avoir une exception, que je n'ai pas.
En visualisant le SQL, je me rend compte qu'il essaye de modifier une ligne inexistante. Mais je ne sais pas qu'il n'y a pas eu de modification.
update Person set version=1 where name='Gavin' and version=0
J'ai surement du oublier un paramétrage, mais lequel ???
Pour illustrer le cas, j'ai rajouter la méthode de test suivante à la classe
org.hibernate.test.version.VersionTest
Code:
public void testVersionCRUU() {
Session s = openSession();
Transaction t = s.beginTransaction();
Person gavin = new Person("Gavin");
s.persist(gavin);
t.commit();
s.close();
assertEquals(0, gavin.getVersion());
Person gavinOld = gavin;
s = openSession();
t = s.beginTransaction();
gavin = (Person) s.createCriteria(Person.class).uniqueResult();
new Thing("Laptop", gavin);
t.commit();
s.close();
assertEquals(1, gavin.getVersion());
s = openSession();
t = s.beginTransaction();
try {
new Thing("New Laptop", gavinOld);
s.saveOrUpdate(gavinOld);
t.commit();
//Normally fail
//see DOC chap 11.3.3. Detached objects and automatic versioning
//http://www.hibernate.org/hib_docs/v3/reference/en/html/transactions.html#transactions-optimistic-detached
fail("gavingOld must not be save, but it is saved ???? ");
} catch (Exception e) {
t.rollback();
} finally {
s.close();
}
s = openSession();
t = s.beginTransaction();
s.createQuery("delete from Thing").executeUpdate();
s.createQuery("delete from Person").executeUpdate();
t.commit();
s.close();
}
Hibernate version: 3.1.2
Name and version of the database you are using:Oracle 9i
The generated SQL (with p6spy):Code:
p6spy - 1138719272273|0|0|statement|insert into Person (version, name) values (?, ?)|insert into Person (version, name) values (0, 'Gavin')
p6spy - 1138719272289|0|0|commit||
p6spy - 1138719272398|93|0|statement|select this_.name as name0_0_, this_.version as version0_0_ from Person this_|select this_.name as name0_0_, this_.version as version0_0_ from Person this_
p6spy - 1138719272398|-1||resultset|select this_.name as name0_0_, this_.version as version0_0_ from Person this_|name0_0_ = Gavin, version0_0_ = 0
p6spy - 1138719272414|0|0|statement|select thing_.description, thing_.version as version1_, thing_.longDescription as longDesc3_1_, thing_.person as person1_ from Thing thing_ where thing_.description=?|select thing_.description, thing_.version as version1_, thing_.longDescription as longDesc3_1_, thing_.person as person1_ from Thing thing_ where thing_.description='Laptop'
p6spy - 1138719272430|0|0|statement|insert into Thing (version, longDescription, person, description) values (?, ?, ?, ?)|insert into Thing (version, longDescription, person, description) values (0, '', 'Gavin', 'Laptop')
p6spy - 1138719272430|0|0|statement|update Person set version=? where name=? and version=?|update Person set version=1 where name='Gavin' and version=0
p6spy - 1138719272430|0|0|commit||
p6spy - 1138719272445|15|0|statement|select person_.name, person_.version as version0_ from Person person_ where person_.name=?|select person_.name, person_.version as version0_ from Person person_ where person_.name='Gavin'
p6spy - 1138719272445|0|0|statement|select thing_.description, thing_.version as version1_, thing_.longDescription as longDesc3_1_, thing_.person as person1_ from Thing thing_ where thing_.description=?|select thing_.description, thing_.version as version1_, thing_.longDescription as longDesc3_1_, thing_.person as person1_ from Thing thing_ where thing_.description='New Laptop'
p6spy - 1138719272461|0|0|statement|insert into Thing (version, longDescription, person, description) values (?, ?, ?, ?)|insert into Thing (version, longDescription, person, description) values (0, '', 'Gavin', 'New Laptop')
p6spy - 1138719272461|0|0|statement|update Person set version=? where name=? and version=?|update Person set version=1 where name='Gavin' and version=0
p6spy - 1138719272461|0|0|commit||