What is difference between session.save() , session.saveOrUpdate() and session.persist()in Hibernate?
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.
Ads