Issue
I was following a tutorial on Hibernate and saw the following code:
package com.websystique.spring.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
public abstract class AbstractDao {
@Autowired
private SessionFactory sessionFactory;
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
public void persist(Object entity) {
getSession().persist(entity);
}
public void delete(Object entity) {
getSession().delete(entity);
}
}
I was wondering if persist()
(or save()
or delete()
) can be used without a transaction? As it seems to be the case here.
Solution
you cant save or persist object without transaction you have to commit the transaction after saving the object otherwise it won't save in database. Without transaction you can only retrieve object from database
Answered By - GAURAV ROY
Answer Checked By - Mary Flores (JavaFixing Volunteer)