Difference between session.save() , session.saveOrUpdate() and session.persist() in Hibernate

Ads

 
 

Share on Google+Share on Google+

Rahul Dev
Difference between session.save() , session.saveOrUpdate() and session.persist() in Hibernate
1 Answer(s)      6 years and 7 months ago
Posted in : Hibernate

What is difference between session.save() , session.saveOrUpdate() and session.persist()in Hibernate?

Ads
View Answers

October 26, 2010 at 3:55 PM


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









Related Tutorials/Questions & Answers:
Tutorials   
Java Spring Hibernate Struts Training Hibernate envers revtype values Hibernate envers audit example Hibernate envers example Hibernate envers revinfo table revinfo table sql Hibernate maven dependency example What is the difference between JPA and Hibernate? How to turn off hbm2ddl? Hibernate hbm2ddl.auto values Hibernate table not mapped error - solution Hibernate error class is not mapped Hibernate Envers custom revinfo table Hibernate Envers - REVINFO table doesn't exist How to enable second level cache in Hibernate 5? How to enable second level cache in Hibernate 5? java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based! org.hibernate.validator cannot be resolved error java.lang.ClassNotFoundException: org.hibernate.cache.TimestampsRegion java.lang.ClassNotFoundException: org.hibernate.cache.TimestampsRegion property hibernate.cache.region.factory_class is not given Hibernate example step by step in Eclipse with MySQL How to make Struts Hibernate and Spring Based Login/Registration Application? What is the use of Hibernate Native SQL? What is the use of Hibernate is used? How to integrate Struts and Hibernate in an application? How to create the program in Java for getting the forgotten Password Form? How to write create JPA Many-to-Many Relationship in Java? What is the DAO Layer in any application? What is Spring Struts Hibernate DAO Layer? How to write Example code for CRUD application in hibernate annotation? Where to update the Hibernate tools? Query conversion convert into hql how to convert sql into hql package org.hibernate.util does not exist C3P0 with Hibernate What do you understand by JPA? Hibernate Query Checker Jpa many to many relationship What do you understand by Hibernate in Java? What are criteria Queries in Hibernate? How to use Associations and Joins in Hibernate? Hibernate 4 Example with Eclipse How to learn Hibernate? What is Hibernate Framework? hibernate faq 15/03/14 hibernate 15/03/2014 Hibernate Criteria Wildcard how to add mulitiple projections this is a hibernate question

Ads

 
Advertisement null

Ads