hussaa wrote:
According to the "Hibernate in Action" book page 127, the sql INSERT statement contains values at the point save() is called. If at any point after this the object is modified, changes are propogated using ans sql UPDATE. Well this seems to be the behavior inherent in Hibernate though its a little surprising.
I just understood why save() has obviously to execute immediately in case of a newly instantiated object. To understand, just look at the
save() signature and javadoc:
Code:
/**
* Persist the given transient instance, first assigning a generated identifier. (Or
* using the current value of the identifier property if the <tt>assigned</tt>
* generator is used.) This operation cascades to associated instances if the
* association is mapped with <tt>cascade="save-update"</tt>.
*
* @param object a transient instance of a persistent class
* @return the generated identifier
* @throws HibernateException
*/
public Serializable save(Object object) throws HibernateException;
Now, it's obvious: the save() method returns the persistent object identifier. So in case of a new object that is mapped to use a db generated id, Hibernate can't return anything without sending the query to the DB to retrieve this generated id (or with generator=identity for example)...
This is certainly one of the main reasons
why the persist() method, defined in the JSR-220, is void... Because Hibernate is forced to issue the insert directly when using save(), not with persist().