I'm trying to use JPA's Context Injection with Hibernate as the engine. Currently, the context fails to be injected (into entityManager - see code below), I get a NullPointerException error:
Mark Spritzler wrote:Yes, in Spring you want to inject the EntityManagerFactory and not the EntityManager. There might be a way to take your EntityManagerFactory and bind it into JNDI where the EntityManager injection takes place outside of Spring, but I have never tried it myself.
Mark
Mark,
I thought that with Spring 2.5 one could simply use @PersistenceContext (as an alternative to Spring's JpaTemplate) annotation on a property to have it injected with the entity manager; thus allowing one to use entity manager directly rather than the factory.
Mark Spritzler wrote:Yes, in Spring you want to inject the EntityManagerFactory and not the EntityManager. There might be a way to take your EntityManagerFactory and bind it into JNDI where the EntityManager injection takes place outside of Spring, but I have never tried it myself.
Mark
Mark,
I thought that with Spring 2.5 one could simply use @PersistenceContext (as an alternative to Spring's JpaTemplate) annotation on a property to have it injected with the entity manager; thus allowing one to use entity manager directly rather than the factory.
-Dan
It might be possible with JPA. With Hibernate I always just inject the SessionFactory then in each method call getCurrentSession() on the sessionFactory, because you can't inject a Session and you wouldn't want to since the Repository is a Singleton and created at startup. Then the Session would hold a connection for as long as the App was running. Not good for pooling.
So I assume JPA is the same in Spring. The Repository is a Singleton and you wouldn't want it to hold onto an EntityManager and Connection for the entire time of the App.