• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

JPA's context injection with Hibernate as engine

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:



 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic