• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA's context injection with Hibernate as engine

 
Dan King
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

Exception in thread "main" java.lang.NullPointerException
at learn.spring.dao.ListingDao.findById(UtilistDao.java:19)


Below is my -- spring configuration, DAO class, & persistence.xml -- anyone see what is wrong/missing? Thanks.

Spring configuration file:



DAO class:


persistence.xml:



 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dan King
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan King wrote:
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.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic