Difference between session.save() , session.saveOrUpdate() and session.persist()
By Ramakrishna on Sep 2, 2008 in hibernate interview questions
Q) Difference between session.save() , session.saveOrUpdate() and session.persist()?
A)
–> session.save() : Save does an insert and will fail if the primary key is already persistent.
–> session.saveOrUpdate() : saveOrUpdate does a select first to determine if it needs to do an insert or an update.
Insert data if primary key not exist otherwise update data.
–> session.persist() : Does the same like session.save().
But session.save() return Serializable object but session.persist() return void.
session.save() returns the generated identifier (Serializable object) and session.persist() doesn’t.
For Example :
if you do :-
System.out.println(session.save(question));
This will print the generated primary key.
if you do :-
System.out.println(session.persist(question));
Compile time error because session.persist() return void.

1 Comment(s)
By Arden Nordquist on Sep 6, 2011 | Reply
Excellent read, I just passed this onto a colleague who was doing a little research on that. And he just bought me lunch since I found it for him smile So let me rephrase that: Thank you for lunch!