- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between save() and persist() in Hibernate
Save() and persist() both methods are used for saving object in the database.
As per docs −
Save() − Persist the given transient instance, first assigning a generated identifier. (Or using the current value of the identifier property if the assigned generator is used.) This operation cascades to associated instances if the association is mapped with cascade="save-update".
As per docs −
persist() − Make a transient instance persistent. This operation cascades to associated instances if the association is mapped with cascade="persist". The semantics of this method are defined by JSR-220.
Sr. No. | Key | save() | persist() |
---|---|---|---|
1 | Basic | It stores object in database | It also stores object in database |
2 | Return Type | It return generated id and return type is serializable | It does not return anything. Its void return type. |
3 | Transaction Boundaries | It can save object within boundaries and outside boundaries | It can only save object within the transaction boundaries |
4. | Detached Object | It will create a new row in the table for detached object | It will throw persistence exception for detached object |
5. | Supported by | It is only supported by Hibernate | It is also supported by JPA |
Example of Save in Hibernate
@Entity public class User { @Id Integer id; String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import com.journaldev.hibernate.util.HibernateUtil; public class SaveExample { public static void main(String[] args) { //get session factory to start transcation SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); User user = new User(); user.setId(1) user.setName("ABCD") session.save(user); //Close resources tx.commit(); sessionFactory.close(); } }
Example of Persist in Hibernate
@Entity public class User { @Id Integer id; String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import com.journaldev.hibernate.util.HibernateUtil; public class PersistExample { public static void main(String[] args) { //get session factory to start transcation SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); User user = new User(); user.setId(1) user.setName("ABCD") session.persist(user); //Close resources tx.commit(); sessionFactory.close(); } }
- Related Articles
- Difference Between JDBC and Hibernate
- Difference between Hibernate and JPA
- Difference Between get() and load() in Hibernate
- Difference between sequence and identity in Hibernate
- Difference between Hibernate and Eclipse link
- Difference between lazy and eager loading in Hibernate
- Difference Between First level cache and Second level cache in Hibernate
- \nDifference between Save and SaveAndFlush in Spring Java\n
- How to Execute Batch Insert Update in Hibernate?
- Creating a table with MySQL - Hibernate
- Difference between \'and\' and \'&\' in Python
- Which one is safer Sleep or Hibernate in Windows?
- What are various Inheritance mapping strategies available in Hibernate?
- How does Hibernate Second Level Cache Works?
- How to connect hibernate with MySQL Database?
