How do you inject a JPA EntityManager(Factory) using JSF on Tomcat?

L

Lew

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.
 
E

EricF

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
 
L

Lew

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.

Actually, adding just the OpenJPA libraries, and the Hibernate libs should
work equivalently, gets me most of the way there.
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.

I am pretty sure that EJB3 annotations will work in the right container even
if the thing isn't otherwise identified as a session bean. I haven't
experimented with that yet so I'll let you know when I do. Currently I can't
even get the darned thing to deploy to Glassfish, though it deploys to Tomcat
just fine.
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. :)

It's on my list now, too.

I found that if I instantiate the EntityManagerFactory explicitly with the
name of the persistence unit, rather than rely on the annotation, that it
picks up the configuration from the persistence.xml and context.xml files just
as I'd hoped, even under Tomcat. I cannot use the Java Transaction API (JTA)
to achieve container-managed transactions, but otherwise I get the notational
convenience and instant-on access to the database for which I'd hoped.

Transaction management undoubtedly will be the next difference, but I'm
accustomed to managing transactions anyway. Making entities with @Entity is
slick as anything.
 
E

EricF

Actually, adding just the OpenJPA libraries, and the Hibernate libs should
work equivalently, gets me most of the way there.


I am pretty sure that EJB3 annotations will work in the right container even
if the thing isn't otherwise identified as a session bean. I haven't
experimented with that yet so I'll let you know when I do. Currently I can't
even get the darned thing to deploy to Glassfish, though it deploys to Tomcat
just fine.

Let me know what you find. I think an EJB container is needed to inject a
persistence unit, and it only injects into an EJB. But I could be wrong.
It's on my list now, too.

I found that if I instantiate the EntityManagerFactory explicitly with the
name of the persistence unit, rather than rely on the annotation, that it
picks up the configuration from the persistence.xml and context.xml files just
as I'd hoped, even under Tomcat. I cannot use the Java Transaction API (JTA)
to achieve container-managed transactions, but otherwise I get the notational
convenience and instant-on access to the database for which I'd hoped.

Transaction management undoubtedly will be the next difference, but I'm
accustomed to managing transactions anyway. Making entities with @Entity is
slick as anything.
Yes, the transaction management is the difference. If you don't mind managing
them you should be fine. Spring can help with that. I need to look at the new
2.5 annotations ...

Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

JSF/JPA problem 12
JPA in practice 7
JPA controller Classes in Netbeans 6.5 ? 0

Members online

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,727
Latest member
FelicaTole

Latest Threads

Top