File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes JPA's context injection with Hibernate as engine Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "JPA Watch "JPA New topic
Author

JPA's context injection with Hibernate as engine

Dan King
Ranch Hand

Joined: Mar 18, 2009
Posts: 84
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Dan King
Ranch Hand

Joined: Mar 18, 2009
Posts: 84
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPA's context injection with Hibernate as engine