users@jersey.java.net

RE: [Jersey] Jersey and JPA best practices?

From: saturon <netbeans_at_vtxfree.ch>
Date: Fri, 19 Dec 2008 22:31:49 +0100

I'm getting a bit crazy here ...

Is there no explanation/best practice out there how JPA & Jersey working
with e.g. glassfish??

My problem stems from:
- the generated class PersistenceService from Netbeans 6.1 "Generate
Restful Webservices..." just does not seem to work with Jersey when I
want to use container injected entitymanager (eclipselink), so that lazy
loading is working.
In my env. Jersey / glassfish did -not- inject entitymanagerfactory it's
always null. Is this because it's a not-jax-rs annotated class - but a
threadlocal singleton util I use all over my resources?

And maybe OT but is it best to always close the entitymanager after any
operation? Closing should not influence cache right? For me however it
seemed it did ..


Please if somebody help me out here ;)

Thanks loads Ben



-----Original Message-----
From: Paul.Sandoz_at_Sun.COM [mailto:Paul.Sandoz_at_Sun.COM]
Sent: Freitag, 19. Dezember 2008 13:38
To: users_at_jersey.dev.java.net
Subject: Re: [Jersey] Jersey and JPA best practices?


Hi Florian,

Scala! :-)

Complete injection support equivalent to that on Servlet is not
currently implemented. It will work by the time EE 6 is finalized.

What servlet class are you using for your web application?

At the end of the email is the web.xml from the bookmark example. To
get this working requires init-params for the persistence unit and we
only support the injection of an EntityManagerFactory. The reason is
there is currently no API to get access to information in the web.xml
like the persistence contexts.

Paul.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
">
     <servlet>
         <servlet-name>Jersey Web Application</servlet-name>
         <servlet-
class>com.sun.jersey.server.impl.container.servlet.ServletAdaptor</
servlet-class>
         <init-param>
             <param-name>com.sun.jersey.config.feature.Redirect</param-
name>
             <param-value>true</param-value>
         </init-param>
         <init-param>
             <param-name>unit:BookmarkPU</param-name>
             <param-value>persistence/bookmark</param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
         <servlet-name>Jersey Web Application</servlet-name>
         <url-pattern>/resources/*</url-pattern>
     </servlet-mapping>
     <session-config>
         <session-timeout>
             30
         </session-timeout>
     </session-config>
     <welcome-file-list>
        <welcome-file>
             index.jsp
         </welcome-file>
     </welcome-file-list>
     <persistence-unit-ref>
         <persistence-unit-ref-name>persistence/bookmark</persistence-
unit-ref-name>
         <persistence-unit-name>BookmarkPU</persistence-unit-name>
     </persistence-unit-ref>
     <resource-ref>
         <res-ref-name>UserTransaction</res-ref-name>
         <res-type>javax.transaction.UserTransaction</res-type>
         <res-auth>Container</res-auth>
     </resource-ref>
</web-app>


On Dec 19, 2008, at 12:47 PM, Florian Hars wrote:

> Paul Sandoz schrieb:
>> I recommend emailing mailto:users_at_glassfish.dev.java.net
>> to ask questions related to the persistence context support in EE.
>
> But my problem is not with persistence in EE in general, but with
> persistence
> with jersey. This code as a servlet works like a charm:
>
> import javax.persistence.{EntityManager,PersistenceContext}
> import javax.servlet.http._
>
> class SUsers extends HttpServlet {
>
> private var em: EntityManager = _
>
> @PersistenceContext{ val unitName="test" }
> def setEntityManager(e: EntityManager) { em = e }
>
> def allUsers = {
> {<html><body><table>
> { for (user <- jpa.User.getAll(em)) yield
> <tr><td>{ user.userName }</td></tr>
> }
> </table></body></html>}.toString
> }
>
> override def service(rq : HttpServletRequest, resp :
> HttpServletResponse) {
> resp setContentType "text/html";
> resp.getWriter() println allUsers
> }
> }
>
> What does not work is the naive translation into a jersey resource,
> there em is
> always null:
>
> import javax.persistence.{EntityManager,PersistenceContext}
> import javax.ws.rs._
> import javax.ws.rs.core._
>
> @Path("user/")
> class Users {
>
> private var em: EntityManager = _
>
> @PersistenceContext{ val unitName="test" }
> def setEntityManager(e: EntityManager) { em = e }
>
> @GET
> @Produces(Array("text/html"))
> def allUsers = {
> {<html><body><table>
> { for (user <- jpa.User.getAll(em)) yield
> <tr><td>{ user.userName }</td></tr>
> }
> </table></body></html>}.toString
> }
> }
>
> Is this even expected to work, or is this one of the things
> scheduled for
> JavaEE 6? The bookmarks example seems to imply that it should work
> (or at least
> equivalent code using an EntityManagerFactory), but it doesn't...
>
> - Florian.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
For additional commands, e-mail: users-help_at_jersey.dev.java.net