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 |
Comments
Post a Comment
Leave Comment