As I understand when I use hibernate with Spring transactions, a session is bound to thread using ThreadLocal. My questions are:
-
How can I access the session or session factory (without injecting it to the bean) (thus by thread locale)?
2.How can I do the same in terms of JPA, thus using EnityManager / EntityManagerFactory?
You should use the SessionFactory.getCurrentSession() to get your Session. What the session returned from this method depends on the configuration parameter
hibernate.current_session_context_classin thehibernate.cfg.xml. If it is set tothread, the returned session is get from theThreadLocal, which means that if it is called for the first time in the current Java thread, a new Session is opened and returned . If it is called again in the same thread , the same session will be returned.AFAIK , there are no equivalent
SessionFactory.getCurrentSession()in JPA . I think you have manually set and get theEntityManagerto theThreadLocalReference