Skip to main content

Reusing EJBs outside J2EE

5 replies [Last post]
dibyendumajumdar
Offline
Joined: 2005-01-15

Since Entity Beans and Session Beans can be coded as POJOs, I figured that it should be possible to write code that works both inside and outside the J2EE container. As a first step to trying this out, I am building a small demo that consists of:

1. The Entities packaged in a jar file.
2. The Session beans packaged in another jar file.
3. An EAR containing above and deployed to Glassfish server.
4. A client application that attempts to access a session bean using J2EE application client interface.
5. A client application that accesses the same session bean but from a regular J2SE environment using application managed Persistence.

My first question: where should I place the persistence.xml file? I have it in the entity jar file but this is causing a problem when I invoke the J2EE application client - the dependency injection in the session bean fails.

My second question: The persistence.xml is supposed to contain "class" entries when used outside of container. However, if these entries are present when attempting to deploy in Glassfish, I get an exception. Why can't Glassfish simply ignore the "class" entries?

Regards

Dibyendu

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ss141213
Offline
Joined: 2005-03-30

> My first question: where should I place the
> persistence.xml file?

In entity jar.

> I have it in the entity jar
> file but this is causing a problem when I invoke the
> J2EE application client - the dependency injection in
> the session bean fails.

What exception are you getting? There is a similar example posted at http://weblogs.java.net/blog/ss141213/archive/2005/12/using_java_pers.html and that works. You may like to check that out.

>
> My second question: The persistence.xml is supposed
> to contain "class" entries when used outside of
> container. However, if these entries are present when
> attempting to deploy in Glassfish, I get an
> exception.
What exception are you getting? It may be a bug.

> Why can't Glassfish simply ignore the
> "class" entries?
Container is not supposed to ignore the entries. Container is supposed to introspect those classes for persistence annotations.

Thanks,
Sahoo

dibyendumajumdar
Offline
Joined: 2005-01-15

Hello Sahoo,

Thanks for the reply.

I have put the persistence.xml in the entity jar file.

I had a look at your sample. Mine is different in that I have created separate Eclipse projects for each component, as I want to model a large scale development environment where different teams work on different layers.

With regards to my first question, I think the answer lies in the packaging of the EAR file. I looked at your sample, and moved the entity jar file into the "lib" folder within the EAR file, and that seems to have resolved the problem.

Second problem is unresolved. It seems that if you deploy a persistence.xml file to the Container, it doesn't accept the "class" elements. Here is a sample persistence.xml file:

oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider
value="oracle.toplink.essentials.platform.database.DerbyPlatform" />
value="org.apache.derby.jdbc.ClientDriver" />
value="jdbc:derby://localhost:1527/sun-appserv-samples;retrieveMessagesFromServerOnGetMessage=true;create=true;" />
entity.tpcc.Warehouse
entity.tpcc.District
entity.tpcc.Customer
entity.tpcc.Item
entity.tpcc.Stock
entity.tpcc.Order
entity.tpcc.OrderLine
entity.tpcc.NewOrder
jdbc/__default

What I'd like to do is use the EntityManager em1 when deploying in the Container, but em2 when accessing the Entities from a J2SE environment. If I try to deply this, the error I get from the verifier is shown below:

Error Name : Problem in parsing the xml file. For Deployment descriptor file META-INF/persistence.xml in archive [tpcc-persist.jar]. cvc-complex-type.2.4.d: Invalid content was found starting with element 'class'. No child element is expected at this point., error at line 18, column 10
Error Description : org.xml.sax.SAXParseException: cvc-complex-type.2.4.d: Invalid content was found starting with element 'class'. No child element is expected at this point.

at com.sun.enterprise.deployment.io.DeploymentDescriptorFile.read(DeploymentDescriptorFile.java:285)

at com.sun.enterprise.deployment.archivist.Archivist.readPersistenceDeploymentDescriptor(Archivist.java:1743)

at com.sun.enterprise.deployment.archivist.ApplicationArchivist.readPersistenceDeploymentDescriptors(ApplicationArchivist.java:922)

at com.sun.enterprise.tools.verifier.VerificationHandler.createApplicationDescriptor(VerificationHandler.java:424)

at com.sun.enterprise.tools.verifier.VerificationHandler.getApplicationDescriptor(VerificationHandler.java:246)

at com.sun.enterprise.tools.verifier.VerificationHandler.initStandalone(VerificationHandler.java:197)

at com.sun.enterprise.tools.verifier.VerificationHandler.(VerificationHandler.java:96)

at com.sun.enterprise.tools.verifier.Verifier.verify(Verifier.java:127)

at com.sun.enterprise.tools.verifier.Verifier.main(Verifier.java:101)

Error Name : Could not verify successfully.
Error Description : java.io.IOException

at com.sun.enterprise.tools.verifier.VerificationHandler.initStandalone(VerificationHandler.java:208)

at com.sun.enterprise.tools.verifier.VerificationHandler.(VerificationHandler.java:96)

at com.sun.enterprise.tools.verifier.Verifier.verify(Verifier.java:127)

at com.sun.enterprise.tools.verifier.Verifier.main(Verifier.java:101)

Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.d: Invalid content was found starting with element 'class'. No child element is expected at this point.

at com.sun.enterprise.deployment.io.DeploymentDescriptorFile.read(DeploymentDescriptorFile.java:285)

at com.sun.enterprise.deployment.archivist.Archivist.readPersistenceDeploymentDescriptor(Archivist.java:1743)

at com.sun.enterprise.deployment.archivist.ApplicationArchivist.readPersistenceDeploymentDescriptors(ApplicationArchivist.java:922)

at com.sun.enterprise.tools.verifier.VerificationHandler.createApplicationDescriptor(VerificationHandler.java:424)

at com.sun.enterprise.tools.verifier.VerificationHandler.getApplicationDescriptor(VerificationHandler.java:246)

at com.sun.enterprise.tools.verifier.VerificationHandler.initStandalone(VerificationHandler.java:197)

... 3 more

I also find that the if I try to run the application in the J2SE environment with both the PUs defined, then I get:

[java] java.lang.reflect.InvocationTargetException
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[java] at java.lang.reflect.Method.invoke(Unknown Source)
[java] at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(Unknown Source)
[java] Caused by: java.lang.reflect.InvocationTargetException
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[java] at java.lang.reflect.Method.invoke(Unknown Source)
[java] at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializerAgent.initializeFromAgent(JavaSECMPInitializerAgent.java:54)
[java] at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializerAgent.premain(JavaSECMPInitializerAgent.java:47)
[java] ... 5 more
[java] Caused by: java.lang.NullPointerException
[java] at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.callPredeploy(JavaSECMPInitializer.java:120)
[java] at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.initPersistenceUnits(JavaSECMPInitializer.java:282)
[java] at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.initialize(JavaSECMPInitializer.java:301)
[java] at oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializer.initializeFromAgent(JavaSECMPInitializer.java:318)
[java] ... 11 more
[java] FATAL ERROR in native method: processing of -javaagent failed
[java] Exception in thread "main"

In both cases, the problem is resolved by commenting out the PU definition that is not needed. However, I cannot see why this should be required.

ss141213
Offline
Joined: 2005-03-30

> Second problem is unresolved. It seems that if you
> deploy a persistence.xml file to the Container, it
> doesn't accept the "class" elements. Here is a sample
> persistence.xml file:

The problem is with your XML file. It is not compliant with the persitence schema ($GLASSFISH_HOME/lib/schemas/persistence.xsd). More details given further below.

>
> deply this, the error I get from the verifier is
> shown below:
>
> Error Name : Problem in parsing the xml file. For
> r Deployment descriptor file META-INF/persistence.xml
> in archive [tpcc-persist.jar].
> cvc-complex-type.2.4.d: Invalid content was found
> d starting with element 'class'. No child element is
> expected at this point., error at line 18, column 10

As per schema, element should be before
element. In your example, you have put it after
.

>
>
> I also find that the if I try to run the application
> in the J2SE environment with both the PUs defined,
> then I get:
>
> [java]
> java] java.lang.reflect.InvocationTargetException
For Java SE, I don't see why you should get such a weired exception. Could be a bug.

> In both cases, the problem is resolved by commenting
> out the PU definition that is not needed. However, I
> cannot see why this should be required.

Hope your inside container problem is fixed after fixing persistence.xml. For outside container use case, please file a bug.

Thanks,
Sahoo

ss141213
Offline
Joined: 2005-03-30

Dibyendu,

Check out http://weblogs.java.net/blog/ss141213/archive/2005/12/how_to_automati.html where a very simple [b]apt[/b] is made available to automatically [b]update[/b] persistence.xml with entries.

-- Sahoo

dibyendumajumdar
Offline
Joined: 2005-01-15

Sahoo,

You were right about the ordering of elements in the persistence.xml file. The Container is now happy with the file, however, when using J2SE I still get a NullPointerException. I will create an issue for this.

Thanks

Dibyendu