Lets discuss some of the Hibernate methods used for save, update, delete, and retrieve operations
- save()
The
save()
method is used to store an object in the database. It inserts a row into the database for the given entity object. It returns the generated identifier.Serializable save(Object object) throws HibernateException
- If the object is already persistent in the current session, it throws an exception.
- It makes a transient instance persistent. However, it does not guarantee that the identifier value will be assigned immediately, the assignment might happen at flush time.
- It also does not guarantee that the
INSERT
statement will be issued immediately, it might happen at flush time.
Example:
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Employee emp = new Employee("John Doe", "Marketing"); session.save(emp); tx.commit(); session.close();
persist()
The
persist()
method is similar tosave()
. It is used to store an object in the database. It also makes a transient instance persistent. However, it does not return the generated identifier.void persist(Object object) throws HibernateException
- It will not affect immediate synchronization with the database.
- If the object is already persistent in the current session, it does not do anything.
Example:
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Employee emp = new Employee("Jane Doe", "Sales"); session.persist(emp); tx.commit(); session.close();
saveOrUpdate()
The
saveOrUpdate()
method either saves the entity object if it’s transient or updates the existing object if it’s persistent.void saveOrUpdate(Object object) throws HibernateException
- If the object is already persistent in the current session, it updates the object.
- If the object is not associated with the session, it saves the object.
Example:
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Employee emp = (Employee) session.get(Employee.class, 1); emp.setDepartment("HR"); session.saveOrUpdate(emp); tx.commit(); session.close();
update()
The
update()
method is used to update the entity’s state to the underlying database. It updates the row of the existing entity object.void update(Object object) throws HibernateException
- If the object is not persistent in the current session, it throws an exception.
- If another object associated with the session has the same identifier, it throws an exception.
Example:
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Employee emp = (Employee) session.get(Employee.class, 1); emp.setDepartment("Finance"); session.update(emp); tx.commit(); session.close();
merge()
The
merge()
method is used to update the entity’s state to the underlying database. It merges the state of the given entity into the current persistence context.Object merge(Object object) throws HibernateException
- If the object is not persistent in the current session, it loads the persistent object with the same identifier and returns it. Then it merges the changes.
- If another object associated with the session has the same identifier, it merges the changes.
Example:
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Employee emp = (Employee) session.get(Employee.class, 1); emp.setDepartment("IT"); session.merge(emp); tx.commit(); session.close();
delete()
The
delete()
method is used to remove a persistent instance from the datastore. It deletes the row of the existing entity object.void delete(Object object) throws HibernateException
- If the object is not persistent in the current session, it throws an exception.
- If there is no row with the identifier, it does nothing.
Example:
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); Employee emp = (Employee) session.get(Employee.class, 1); session.delete(emp); tx.commit(); session.close();
get()
and load()
methods are used to retrieve an entity from the database.
get()
method returnsnull
if the object is not found.load()
method throwsObjectNotFoundException
if the object is not found.get()
method always hits the database.load()
method does not hit the database until you access any property of the object, it returns a proxy.
Example:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Employee emp1 = (Employee) session.get(Employee.class, 1);
Employee emp2 = (Employee) session.load(Employee.class, 2);
tx.commit();
session.close();
In summary, save()
, persist()
, and saveOrUpdate()
are used for saving an entity. update()
, merge()
, and saveOrUpdate()
are used for updating an entity. delete()
is used for deleting an entity. get()
and load()
are used for retrieving an entity.
Post a Comment