🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In Hibernate, both save and persist methods are used to save an object into the database, but there are subtle differences between the two that may influence which one you choose to use in different situations. In this post, we will discuss the difference between Session.save() and Session.persist() methods in Hibernate.
save Method
Return Value: Returns the generated identifier (primary key) of the object.
Behavior Outside a Transaction: If called outside of a transaction, save may still insert the object into the database, depending on the underlying configuration and flush settings.
Identifier Generation: If the object's identifier is not set, Hibernate will generate one based on the defined generation strategy.
Use Case: Use this method when you want to save a new entity and may need the generated identifier immediately after the save.
persist Method
Return Value: Returns void, not the identifier.
Behavior Outside a Transaction: If called outside of a transaction, persist will generally not insert the object immediately but will wait for a transaction to be started. The object must be persisted by the time the transaction is committed, or an exception may be thrown.
Identifier Generation: Similar to save, if the object's identifier is not set, Hibernate will generate one.
Guarantee of Synchronization: Ensures that the persisted state is synchronized with the current transaction.
Use Case: Use this method when you want to save a new entity within the context of a transaction and do not need the identifier immediately.
save and persist Method Examples
Session.save() method example:
In the below example, Session.save() method is called after the transaction.commit() but it executes the INSERT statement and it will insert the record into the database:
public void saveExample(Student student) {
Transaction transaction = null;
try (Session session = HibernateJavaConfig.getSessionfactory().openSession()) {
// start the transaction
transaction = session.beginTransaction();
// commit transaction
transaction.commit();
// save student entity
Serializable id = session.save(student);
System.out.println("database table id -> " + id);
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
}
Session.persist() method example:
In the below example, the Session.persist() method is called after the transaction.commit() but it does not execute the INSERT statement and it won't insert the record into the database:
public void persistExample(Student student) {
Transaction transaction = null;
// try with resource statement to auto close session object
try (Session session = HibernateJavaConfig.getSessionfactory().openSession()) {
// start the transaction
transaction = session.beginTransaction();
// commit transaction
transaction.commit();
// persist student entity
session.persist(student);
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
}
Summary
Here's a table summarizing the key differences:
| Feature | save() Method | persist() Method |
|---|---|---|
| Return Value | Returns the generated identifier | Returns void |
| Behavior Outside Transaction | May insert immediately | Waits for a transaction |
| Identifier Generation | Generates if not set | Generates if not set |
| Guarantee of Synchronization | No specific guarantee | Ensures synchronization with the current transaction |
| Use Case | When an immediate identifier is needed | Within the transaction context, no immediate identifier is needed |
Related Posts
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment