🎓 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
Learn the Hibernate ORM framework at Learn Hibernate Framework
save Method
Behavior: Inserts the object into the database.
Identifier Generation: If the object's identifier (primary key) is not set, Hibernate will generate one based on the defined generation strategy.
Exception Handling: If the object with the same identifier already exists in the database, calling save will result in a primary key violation, leading to an exception.
Use Case: Typically used when you want to save a new entity and are relying on Hibernate to generate the identifier.
saveOrUpdate Method
Behavior: Depending on the object's state, this method will either save a new object or update an existing one.
Identifier Checking: If the identifier is null, it considers the object as transient and will save it. If the identifier is not null, it will check the session to see if the object is already associated; if it is, it will update the existing object.
Exception Handling: This method does not generally throw an exception due to identifier conflicts, as it will update the existing record if it finds one.
Use Case: Use this method when you want to either save a new entity or update an existing one, and you are not sure whether the object is already present in the database or not.
Session save and saveOrUpdate Method Examples
Session.save() method example:
public void saveExample(Student student) {
Transaction transaction = null;
try (Session session = HibernateJavaConfig.getSessionfactory().openSession()) {
// start the transaction
transaction = session.beginTransaction();
// save student entity
Serializable id = session.save(student);
System.out.println("database table id -> " + id);
// commit transaction
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
}
}
Session.saveOrUpdate() method example:
public void saveOrUpdateExample(Student student) {
Transaction transaction = null;
try(Session session = HibernateJavaConfig.getSessionfactory().openSession()){
// start the transaction
transaction = session.beginTransaction();
// first save student entity using saveOrUpdate() method
session.saveOrUpdate(student);
// get the student object
Student student2 = session.get(Student.class, student.getId());
// update the student2 object
student2.setFirstName("Prabhas");
student2.setEmail("prabhas@gmail.com");
// update student entity using saveOrUpdate() method
session.saveOrUpdate(student2);
// commit transaction
transaction.commit();
}catch (Exception e) {
if(transaction != null) {
transaction.rollback();
}
}
Summary
| Feature | save() Method | saveOrUpdate() Method |
|---|---|---|
| Behavior | Inserts the object | Inserts or updates the object |
| Identifier Generation | Generates if not exist | Saves if null, updates if not null |
| Exception Handling | Throws an exception if the identifier exists | Updates if identifier exists |
| Use Case | Saving new entities | Saving new entities or updating existing ones |
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
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment