Hi,
I'm not sure about this behavior, so i'd like someone to confirm (or not).
This is a simple sample code to illustrate the problem. I have a class, Compte, with id of type long and with unsaved-value = "-1". (I use DAO generated by hibernateSynchronizer...)
If i get an existing object c1 from DB and then call DAO.save(c1) on it, a new object with same properties value as c1 will be created with a new id value !!!
There is no care taken on the id value which is NOT unsaved-value.
The code is only
Compte c = compteDAO.load(id); => session.load(class, id);
// modify c...
compteDAO.save(c); => session.save(c);
My feeling is that session.save() suppose the Object is a new one and generate a new id for it (in my case) without regarding if its effectively a new object or not ....
What makes me hesitate is that when I started to use hibernate, I think I have tested that and I think (I'm not really sure) it was not the case : save() generated an exception if trying to save a existing object ...
I know there is saveOrUpdate() method but it's to understand save() behavior...
Thanks for your help
Regards
Veronique
Hibernate version: 2.1.8
+ PostGreSql 7.3
Mapping documents:
<hibernate-mapping package="test.sh">
<class name="Compte" table="compte">
<id column="id" name="id" type="long" unsaved-value="-1">
<generator class="vm" />
</id>
<property column="credit" name="credit" not-null="false" type="double" />
</class>
</hibernate-mapping>
|