These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: persist() and save() behave = regardles of transaction
PostPosted: Sat Sep 27, 2008 12:22 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
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()?

_________________
Gonzalo Díaz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.