Hi all, I've set up a small hands-on webapp with one JPA entity and one REST service. Database gets created on TomEE startup and app is successfully deployed and reachable.
Although this should be quite straightforward, I still have two problems with this: 1.) The OpenJPA user's guide says here <http://openjpa.apache.org/docs/openjpa-0.9.0-incubating/manual/manual.html#ref_guide_mapping_synch> that the mapping gets done when the application obtains its first EM. This is somehow not working for me -- not until I make some random call on the injected entity manager (see method getEntityManagerProperties in HelloRest.java). 2.) After the first call on the EM and the creation of the table for HelloEntity, one might think that everything is set now to stick some rows in ... but this fails because there is no active transaction when calling em.persist(). As of my understanding, the lifecycle of the EM should be managed by the container, so I should't need to hassle with any EntityManagerFactory, Transactions and soon. Any thoughts on what could cause this weird behaviour? Attached below is all that's involved: *tomee.xml* <tomee> <Resource id="test_ds" type="DataSource"> JdbcDriver org.apache.derby.jdbc.ClientDriver JdbcUrl jdbc:derby://localhost:1527/test_db;create=true UserName user Password password JtaManaged true InitialSize 5 </Resource> </tomee> BTW, the database gets created only if I set the resource's InitialSize property to a value > 0 Maybe this way I'm forcing a database connection and that's the only reason I get at least the database created. *persistence.xml* <persistence ...> <persistence-unit name="test_pu" transaction-type="JTA"> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <jta-data-source>test_ds</jta-data-source> <non-jta-data-source>test_ds_unmanaged</non-jta-data-source> <class>de.mdesign.hellorest.entity.HelloEntity</class> <properties> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/> </properties> </persistence-unit> </persistence> *Entity* @Entity public class HelloEntity implements Serializable { @Id @GeneratedValue private Long id; @Column private String name; ... } *App* @Path("/HelloRest") @Produces({MediaType.TEXT_PLAIN}) public class HelloRest { @PersistenceContext(unitName = "test_pu") private EntityManager em; @GET @Path("/props") public String getEntityManagerProperties() { Map<String, Object> emProps = em.getProperties(); // INVOKES THE MAPPING TOOL String ret = ""; for(String key : emProps.keySet()) ret += key+" = "+emProps.get(key)+"\n"; return ret; } @PUT @Path("/create") public String createEntry(@QueryParam("name") String name) { HelloEntity hello = new HelloEntity(); hello.setName(name); em.persist(hello); // FAILS WITH TransactionRequiredException return ("Hello "+hello.getName()+" (id="+hello.getId()+")"); } } *Here's the trace of the persist failure:* 2013-03-04 00:36:39,365 - ERROR - Error occurred during error handling, give up! org.apache.cxf.interceptor.Fault at org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:162) at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:128) at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:167) at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:94) at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58) at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:94) at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262) at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121) at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:236) at org.apache.openejb.server.cxf.rs.CxfRsHttpListener.onMessage(CxfRsHttpListener.java:70) at org.apache.openejb.server.rest.RsServlet.service(RsServlet.java:53) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:45) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:722) Caused by: javax.persistence.TransactionRequiredException at org.apache.openejb.persistence.JtaEntityManager.assertTransactionActive(JtaEntityManager.java:94) at org.apache.openejb.persistence.JtaEntityManager.persist(JtaEntityManager.java:123) at de.mdesign.hellorest.service.HelloRest.createEntry(HelloRest.java:39) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:180) at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96) ... 27 more -- View this message in context: http://openejb.979440.n4.nabble.com/Container-Managed-EM-on-TomEE-with-OpenJPA-tp4661203.html Sent from the OpenEJB User mailing list archive at Nabble.com.
