Application-Managed Persistence - No Persistence Provider for EntityManager

Is it possible to use Application-Managed Persistence in conjuction with

Container-Managed persistence? I am having issues gettting

application-managed persistence to work within a J2EE environment using

Sun Application Server 9.0 w/ Toplink persistence provider.

Here is the design that I have to use:

I have a lot of EJBs that use the same persistence unit that I have setup in

my persistence.xml (I will include it below) and they work fine. However, I

would like to use an entity class that will not be used within EJBs sessions

(instead within POJOs)... my understanding is that the POJO I would like to

use to manage this particular entity class cannot use the injected

container-managed persistence that I used in my EJBs. I checked out the

EJB Perstistence Specs (pg.116) to come up with retrieving the

EntityManager in this way for J2SE applications, however, I get an error

message.

EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("MyPU");

EntityManager em = emf.createEntityManager();

My Error: (from ExceptionObj.getMessage)

===========================================================

ERROR: No Persistence providerfor EntityManager named MyPU

persistence.xml

(the class my.package.MyClass is the one to be managed within a POJO)

===========================================================

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

<persistence-unit name="MyPU" transaction-type="JTA">

<jta-data-source>jdbc/ptp</jta-data-source>

<class>my.package.EJBClass</class>

<class>my.package.MyClass</class>

<exclude-unlisted-classes>true</exclude-unlisted-classes>

<properties/>

</persistence-unit>

</persistence>

I know there is a "provider" tag for the persistence.xml file... I have tried:

<provider>oracle.toplink.essentials.PersistenceProvider</provider>

and

<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>

both of these are listed in toplink-essentials.jar, file:

META-INF/services/javax.persistence.spi.PersistenceProvider

Any help would be appreciated!

EJB 3.0 Persistence Specs

==========================================================

(file: ejb-3_0-fr-spec-persistence.pdf) at URL: http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html

[3103 byte] By [kjsempirea] at [2007-10-3 3:29:14]
# 1

Are you talking about about supporting/utility classes that are running within an ejb invocation or classes that are running in a non Java EE environment?In the former case, it's not that they can't use container-managed persistence contexts. It's just that the @PersistenceContext annotation is only valid on certain container-managed classes such as EJB bean classes.

However, you always have the option of looking up a particular resource dependency within the private component environment (java:comp/env) of the component in which your supporting code is executing.

E.g. if your supporting code is called from a stateless session bean that has a @PersistenceContext :

@Stateless

public class FooBean implements Foo {

@PersistenceContext(name="em_reference") EntityManager em;

public void foo() {

BarUtil barUtil = new BarUtil();

barUtil.bar();

}

...

}

BarUtil can lookup the same container-managed persistence context as follows :

public class BarUtil {

public void bar() {

InitialContext ic = new InitialContext();

EntityManger em = (EntityManager) ic.lookup("java:comp/env/em_reference");

...

}

// Note that the portion of the lookup string after "java:comp/env/" matches the name() attribute of the @PersistenceContext.

ksaksa at 2007-7-14 21:23:00 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

I am talking about a support/utility class that are running within some ejb invocation... thank you for clearing up the situation. I figured that since it was still running within a JEE Env that I could get to the EntityManager through other classes, but couldn't figure out the correct syntax.

This was a big help to me, I appreciate it. I spent numerous hours trying different permutations of injections and inital context lookups; and honestly, when I saw your response I had thought that I had already tried it.:) apparently not...

Thanks again.

kjsempirea at 2007-7-14 21:23:00 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Glad to help. You might also want to take a look at our EJB 3.0 Quiz:

http://java.sun.com/developer/Quizzes/ejb/ejb3.0_simplified_api.html

It covers the core simplified API portion of EJB 3.0 rather than the Persistence API but it touches on some good topics.You can find additional info on the ejb and persistence API glassfish pages :

https://glassfish.dev.java.net/javaee5/ejb/

https://glassfish.dev.java.net/javaee5/persistence/

--ken

ksaksa at 2007-7-14 21:23:00 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...