getCurrentSession
vs openSession
public Session openSession() throws HibernateException
Opens a NEW session from SessionFactory, which has to be explicitly closed by programmer.
public Session getCurrentSession() throws HibernateException
Opens new session if one doesn’t exist, otherwise continues with the existing one.
Gets automatically closed upon Tx boundary (commit/rollback) or thread over (since current session is bound to current thread as specified in hibernate.cfg.xml property current_session_context_class=thread
)
public Serializable save(Object o) throws HibernateException
(Deprecated in Hibernate 6)
@GeneratedValue
is used for id)public void persist(Object transientRef)
org.hibernate.PersistentObjectException: detached entity passed to persist
public void saveOrUpdate(Object ref)