I read that there is a way to accomplish runtime enhancement for JPA
entities.  Is there an example or documentation for this?

I'm VERY NEARLY done with this example application.  I think that I've
finally got the JTA resources correctly configured in Tomcat, and with
David's corrections on the JNDI contexts earlier this week, the application
seems to be getting closer to the finish line with each new error message.

I simplified my example to three components:
  * A collapsed ear / WAR for client code.  The WAR has a servlet and a
stateless session bean that reference the following two component libraries:
  * first.jar, which contains two classes.  "A" and "B".  "A" is an abstract
mapped superclass of "B".  "A" has a single String field, and "B" has a
single long @Id field.
  * second.jar, which contains one class, "C".  "C" is identical to "B"
aside from its classname and that it is packaged into a different JAR.

We put the mysql-connector-java-5.1.5-bin.jar file into the tomcat/lib
directory, so the MySQL connection is on the classpath.  For a while I was
getting errors regarding the MySQL driver, but this solved those problems.

My new servlet looks like this:

public class ExampleServlet extends HttpServlet {
>
>     @EJB
>     Manager ejb;
>
>     public void doGet(HttpServletRequest req,
>                       HttpServletResponse res)
>                      throws IOException {
>
>         res.setContentType("text/plain");
>         ServletOutputStream out = res.getOutputStream();
>
>         out.println("Hello World!");
>
>         B b = new B();
>         b.setName("B");
>
>         out.println(ejb.persist(b));
>     }
> }
>

The persistence.xml file now looks like this:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"; version="1.0">
>  <persistence-unit name="example" transaction-type="JTA">
>
>    <jta-data-source>java:openejb/Resource/myDataSource</jta-data-source>
>
>  
> <non-jta-data-source>java:openejb/Resource/myNonJtaDataSource</non-jta-data-source>
>
>    <class>A</class>
>    <class>B</class>
>    <class>C</class>
>
>  </persistence-unit>
> </persistence>
>

I was having trouble with the EntityManager in a JTA context, but got around
that by reading the JPA spec, section 5.5.  So now my stateless bean looks
something like this:

@Stateless
> public class Persistor implements Manager {
>
>     @PersistenceUnit(name="example")
>     private EntityManagerFactory emf;
>
>     private EntityManager em;
>
>     public String persist(B b) {
>         em = emf.createEntityManager();
>         em.joinTransaction();
>         em.persist(b);
>         em.flush();
>         em.clear();
>
>         return "Persisted " + b + " using " + em;
>     }
> }
>

That made the errors regarding JTA and the EntityManager go away.  Now I'm
having trouble with the classes not being enhanced.  This is a confusing
error since the last few times I've successfully gotten entity components to
persist I didn't need to enhance them.

The stack trace follows.  Any thoughts or references to a solution are much
appreciated.

Cheers!
--
Alexander

javax.ejb.EJBException: The bean encountered a non-application exception.;
> nested exception is:
> <openjpa-1.0.1-r420667:592145 nonfatal user error>
> org.apache.openjpa.persistence.ArgumentException: Attempt to cast instance
> "[EMAIL PROTECTED]" to PersistenceCapable failed.  Ensure that it has been
> enhanced.
> FailedObject: [EMAIL PROTECTED]
> org.apache.openejb.core.ivm.BaseEjbProxyHandler.convertException(
> BaseEjbProxyHandler.java:366)
> org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(
> BaseEjbProxyHandler.java:323)
> org.apache.openejb.util.proxy.Jdk13InvocationHandler.invoke(
> Jdk13InvocationHandler.java:49)
> $Proxy33.persist(Unknown Source)
> ExampleServlet.doGet(ExampleServlet.java:31)
> javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
> javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
>
> root cause
>
> <openjpa-1.0.1-r420667:592145 nonfatal user error>
> org.apache.openjpa.persistence.ArgumentException: Attempt to cast instance
> "[EMAIL PROTECTED]" to PersistenceCapable failed.  Ensure that it has been
> enhanced.
> FailedObject: [EMAIL PROTECTED]
> org.apache.openjpa.kernel.BrokerImpl.assertPersistenceCapable(
> BrokerImpl.java:4285)
> org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2364)
> org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2224)
> org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java
> :1005)
> org.apache.openjpa.persistence.EntityManagerImpl.persist(
> EntityManagerImpl.java:541)
> Persistor.persist(Persistor.java:17)
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java
> :39)
> sun.reflect.DelegatingMethodAccessorImpl.invoke(
> DelegatingMethodAccessorImpl.java:25)
> java.lang.reflect.Method.invoke(Method.java:585)
>
> org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke
> (ReflectionInvocationContext.java:146)
> org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(
> ReflectionInvocationContext.java:129)
> org.apache.openejb.core.interceptor.InterceptorStack.invoke(
> InterceptorStack.java:67)
> org.apache.openejb.core.stateless.StatelessContainer._invoke(
> StatelessContainer.java:203)
> org.apache.openejb.core.stateless.StatelessContainer.invoke(
> StatelessContainer.java:165)
> org.apache.openejb.core.ivm.EjbObjectProxyHandler.businessMethod(
> EjbObjectProxyHandler.java:223)
> org.apache.openejb.core.ivm.EjbObjectProxyHandler._invoke(
> EjbObjectProxyHandler.java:77)
> org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(
> BaseEjbProxyHandler.java:321)
> org.apache.openejb.util.proxy.Jdk13InvocationHandler.invoke(
> Jdk13InvocationHandler.java:49)
> $Proxy33.persist(Unknown Source)
> ExampleServlet.doGet(ExampleServlet.java:31)
> javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
> javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
>

Reply via email to