2

I switched from using Hibernate API to the JPA API. Now im wondering because EntityMangager(Factory) does not feature any SessionFactory#getCurrentSession like methods.

Is there any build in comparable approach with EntityManger?

JWill
  • 105
  • 11
  • 1
    Can you give an example what you would do with the session once you have it? You can probably accomplish everything with entity manager, I have never had the need to use session directly – Predrag Maric Jan 22 '15 at 10:03
  • Im using the sesson per request pattern, managing the sessions in a ServletFilter and accessed them with SessionFactory#getCurrentSession. – JWill Jan 22 '15 at 10:09

1 Answers1

-2

You can get the session using Session session = entityManager.unwrap(Session.class), where Session is a org.hibernate.Session.

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
  • 2
    I´dont need an explicit Session I just need the "current Persistence Object" (=EntityManager) used in the http Request Scope (e.g. Threadlocal scope). As described this was achieved by implementing the Session per Request pattern and using SessionFactory#getCurrentSession before moving to EntityManager – JWill Jan 23 '15 at 08:35
  • You could do that using `ThreadLocal`s, take a look at [this post](http://stackoverflow.com/a/15159818/4074715) for more info – Predrag Maric Jan 23 '15 at 21:34