This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Spring and the fly likes How to inject EntityManager via setter in spring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to inject EntityManager via setter in spring" Watch "How to inject EntityManager via setter in spring" New topic
Author

How to inject EntityManager via setter in spring

Bobby Anderson
Ranch Hand

Joined: Oct 28, 2008
Posts: 114
I want to inject an entity manager into a spring bean using a setter but in my config I only have access to a object of type EntityManagerFactory

I want the following:


In my spring config I get the entity manager factory from JNDI:


So when I want to inject that entityManager into my spring bean I use the following:


Which calls the setter on my bean but I of course get an exception saying that I cannot set a type of EntityManger using a EntityManagerFactory type
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

You need to inject the EntityManagerFactory, then in your methods call the getEntityManager() of the factory. This is needed because you can't create an EntityManager up front, with a Connection, and have your Repository hold on to it forever. This is bad resource holding. So it isn't possible for you to inject an EntityManager.

I hope that helps and makes sense. It is an extremely correct and best implementation to not allow you to inject an EntityManager.

Good Luck

Mark


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

Joined: Mar 23, 2010
Posts: 57
Mark Spritzler wrote:You need to inject the EntityManagerFactory, then in your methods call the getEntityManager() of the factory. This is needed because you can't create an EntityManager up front, with a Connection, and have your Repository hold on to it forever. This is bad resource holding. So it isn't possible for you to inject an EntityManager.

I hope that helps and makes sense. It is an extremely correct and best implementation to not allow you to inject an EntityManager.

Good Luck

Mark


Hi Mark, I was under the impression that the EM injected by spring or via the @PersistenceContext is a proxy only. I'm concerned now because in my code I
inject the EM via the @PersistenceContext annotation, managed by spring. Can you clarify? Do I need to change my code to inject a factory and grab the EM from the factory instead?

thanks...

billworth
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to inject EntityManager via setter in spring
 
Similar Threads
Generic DAO using Spring
Beginner needs help with Spring + JPA + Transactions
JPA's context injection with Hibernate as engine
RestFul API - links/hrefs to composite objects
spring wont inject the entitymanager