Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: persist() versus save()
PostPosted: Wed Dec 14, 2005 11:25 am 
Regular
Regular

Joined: Tue Nov 16, 2004 6:36 pm
Posts: 62
Location: Zürich
Hibernate version: 3.1

Hello,

could someone tell me what the difference between persist() and save() is? The manual only says
Quote:
You may also use persist() instead of save(), with the semantics defined in the EJB3 early draft.
but I don't really know what the EBJ3 early draft says about this.

TIA,

Peter


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 4:35 pm 
Regular
Regular

Joined: Tue Nov 16, 2004 6:36 pm
Posts: 62
Location: Zürich
I don't often reply to myself, but please at least tell me if my question is too stupid or too difficult. The manual only gives one vague hint to persist() versus save(), I'd really like to know what the difference is.

The migration guide doesn't mention I should now use persist() instead of save(), but all examples in the 3.1 manual seem to be using persist not. Is there any difference?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 4:49 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Read the API documentation. Use whatever you like best.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 5:00 pm 
Regular
Regular

Joined: Tue Nov 16, 2004 6:36 pm
Posts: 62
Location: Zürich
christian wrote:
Read the API documentation. Use whatever you like best.

OK, reading the latest API documentation it still seems the same, except:

For persist() it says
Quote:
The semantics of this method are defined by JSR-220.
which doesn't tell me much.

Also, for save() it says:
Quote:
first assigning a generated identifier.
.

Also save() returns the generated identifier (which you mostly ignore) and persist doesn't.

So, does this "semantics defined by JSR-200" mean anything special (as compared to save())?

Does persist() not assign the generated identifier? I can hardly imagine, but the javadoc doesn't seem consistent in that case.

Thanks,

Peter


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 6:25 pm 
Beginner
Beginner

Joined: Wed Nov 30, 2005 2:41 pm
Posts: 29
i went to find JSR-220, got to download the pdf ... note that this is still in draft status, which means it's still subject to change. I didnt want to read the whole 190 pages ... section 3.2.1 talks about persist, in particular:

"If X is a new entity, it becomes managed. The entity X will be entered into the database at or before transaction commit or as a result of the flush operation."

So what does that mean about identifiers? After some more searching, i found this note:
"A managed entity instance is an instance with a persistent identity that is currently associated with a persistence context."

I would infer from "persistent identity" that an identifier has been assigned.

But it would be nice if the API doc could just say that instead of referring us to a 190-page document.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 6:30 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I've pinged Emmanuel a few weeks ago with the same question. It seems like they didn't all agree on that, hence, the persist() method doesn't return an identifier. It definitely has to be resolved, because an object in persistent state is supposed to have an identifier value. I'd expect that the method signature changes before final release of the spec.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 6:32 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Oh, and Hibernate only cascades saveOrUpdate() (or both separate) at flush time, not persist(). EntityManager cascades persist() at flush time. This is documented. Quite a significant difference. My recommendation is to not use persist() on Session, there is no benefit here.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 6:39 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://opensource2.atlassian.com/projec ... e/HHH-1273


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 7:43 pm 
Beginner
Beginner

Joined: Wed Nov 30, 2005 2:41 pm
Posts: 29
christian wrote:
Oh, and Hibernate only cascades saveOrUpdate() (or both separate) at flush time, not persist()...


Not sure exactly what you meant, just the timing? or that persist does not cascade at all? The current hibernate documentation indicates that Session.persist() does cascade as long as 'cascade="persist"' is specified. But it seems to indicate that the cascading is done at the time of the persist() call, not at flush time.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 8:04 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
That is exactly what I wrote and what is repeated in the reference documentation, section about "flushing".


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 7:58 am 
Regular
Regular

Joined: Tue Nov 16, 2004 6:36 pm
Posts: 62
Location: Zürich
christian wrote:
http://opensource2.atlassian.com/projects/hibernate/browse/HHH-1273


Thanks for the clarification. At least this tells me that persist() is still somewhat ill defined, and I'd better stay away from it and keep using save() for the time being.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 9:04 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
In case anybody finds this thread...

persist() is well defined. It makes a transient instance persistent. However, it doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time. The spec doesn't say that, which is the problem I have with persist().

persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. This is useful in long-running conversations with an extended Session/persistence context.

A method like persist() is required.

save() does not guarantee the same, it returns an identifier, and 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. This is not good in a long-running conversation with an extended Session/persistence context.


Top
 Profile  
 
 Post subject: Re: persist() versus save()
PostPosted: Thu Nov 10, 2011 1:09 am 
Beginner
Beginner

Joined: Sun Jan 11, 2009 2:47 pm
Posts: 21
Sadly, 5 years later, this thread still remains the only clear source of information about this subject. The Hibernate documentation, while verbose, is void of all but the most trivial usage information. Why christian's last post is not in the Session javadoc is just another Hibernate documentation mystery.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

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.