Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:3.3.1 GA
Mapping documents:irrelevant, except that the id of A is uuid-autogenerated
Name and version of the database you are using:mckoi
The following simple client code
Code:
SessionFactory sef=cfg.buildSessionFactory();
Session session=sef.openSession();
Transaction trx=session.getTransaction();
trx.begin();
A a=new A();
session.save(a);
System.out.println(a.getId());
trx.commit();
always produces the same output: first generates the ID, and only when the transaction is committed a SQL INSERT is generated.
Code:
402881e51ca1fa49011ca1fa4f1d0001
Hibernate: insert into A (id) values (?)
If I use persist() instead of save(), the same happens.
If I don't wrap the code in a transaction, and simply flush() at the end, again the same output, regardless of whether I use persist() or save().
Doesn't this contradict the the documentation, at
10.2. Making objects persistent , when discussing the differences between save() and persist(), that:
Quote:
persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries.
<-- apparently not true, persist() still works if I remove the transaction code.
Quote:
save() does guarantee to return an identifier. If an INSERT has to be executed to get the identifier ( e.g. "identity" generator, not "sequence"), this INSERT happens immediately, no matter if you are inside or outside of a transaction
<-- apparently not true, save() seems to wait until the transaction is committed or the session is flushed.
Why this simple piece of code fails to demonstrate to me any difference between persist() and update()?