On Oct 9, 2009, at 9:50 AM, tarjei wrote:
> Hi,
>
> (this is partly OT)
>
> On 10/09/2009 09:00 AM, Paul Sandoz wrote:
> > The problem is that by default Jersey does not currently support
> > injection of EE resources. This will be fixed with the release of
> > GlassFish v3 in November.
>
> This explains why I didn't get JPA working when I tried this
> earlier. I ended up using "normal" hibernate with spring to inject
> DAO objects.
>
> An important thing to remember when using Spring this way is to
> configure the resource scopes correctly or you can get odd bugs you
> didn't expect.
>
> @Component
> @Scope("request")
> public class MyResource {
>
> @Resource(name = "MyDao")
> private MyDao myDao;
>
> }
>
> Question: Will this work together with @Transactional? I'm using
> Mysql MyISAM tables at the moment so it is not important to me, but
> I plan to switch to inodb to get transactions and I'm wondering how
> much I must change my code.
It should, IIRC other developers have been using that.
>
> Paul: Would it be possible to get either a wiki page describing how
> to use JPA and/or Hibernate together with Jersey? There are quite
> some pitfalls in this field and I think it is a very common
> combination.
>
Do you want to create one?
My preference is for others who have had more practical experience
with Hibernate/JPA than I (which should not be hard!) to collaborate
on a wiki page, and perhaps contribute a maven sample or three?
Any one can add/modify the Jersey wiki pages if they create an account.
Paul.
> Regards,
> Tarjei
>
> On 10/09/2009 09:00 AM, Paul Sandoz wrote:
>> Hi Ron,
>>
>
>>
>> In the interim it is possible to use the
>> com.sun.jersey.server.impl.container.servlet.ServletAdaptor or use
>> Jersey's injection mechanism in the interim. I have attached the
>> source
>> below if you want to copy the source and extend from ServletContainer
>> (the public API) instead.
>>
>> Paul.
>>
>> package com.sun.jersey.server.impl.container.servlet;
>>
>> import com.sun.jersey.api.container.ContainerException;
>> import com.sun.jersey.api.core.ResourceConfig;
>> import com.sun.jersey.spi.container.WebApplication;
>> import com.sun.jersey.spi.container.servlet.ServletContainer;
>> import com.sun.jersey.spi.inject.Injectable;
>> import com.sun.jersey.spi.inject.InjectableProvider;
>> import com.sun.jersey.core.spi.component.ComponentContext;
>> import com.sun.jersey.core.spi.component.ComponentScope;
>> import java.lang.reflect.Proxy;
>> import java.lang.reflect.Type;
>> import java.util.Enumeration;
>> import java.util.HashMap;
>> import java.util.Map;
>> import javax.persistence.EntityManagerFactory;
>> import javax.persistence.PersistenceUnit;
>> import javax.servlet.ServletConfig;
>>
>> /**
>> * A servlet container for deploying root resource classes with
>> support
>> * for injecting persistence units.
>> * <p>
>> * Persistence units that may be injected must be configured in
>> web.xml
>> * in the normal way plus an additional servlet parameter to enable
>> the
>> * Jersey servlet to locate them in JNDI. E.g. with the following
>> * persistence unit configuration:
>> *
>> * <persistence-unit-ref>
>> * <persistence-unit-ref-name>persistence/widget</persistence-unit-
>> ref-name>
>> * <persistence-unit-name>WidgetPU</persistence-unit-name>
>> * </persistence-unit-ref>
>> *
>> * the Jersey servlet requires an additional servlet parameter as
>> * follows:
>> *
>> * <init-param>
>> * <param-name>unit:WidgetPU</param-name>
>> * <param-value>persistence/widget</param-value>
>> * </init-param>
>> *
>> * Given the above, Jersey will inject the EntityManagerFactory found
>> * at java:comp/env/persistence/widget in JNDI when encountering a
>> * field or parameter annotated with
>> @PersistenceUnit(unitName="WidgetPU").
>> */
>> public class ServletAdaptor extends ServletContainer {
>> private Map<String, String> persistenceUnits =
>> new HashMap<String, String>();
>> @Override
>> protected void configure(ServletConfig servletConfig,
>> ResourceConfig rc,
>> WebApplication wa) {
>> super.configure(servletConfig, rc, wa);
>>
>> /**
>> * Look for persistent units.
>> */
>> for (Enumeration e = servletConfig.getInitParameterNames() ;
>> e.hasMoreElements() ;) {
>> String key = (String)e.nextElement();
>> String value = servletConfig.getInitParameter(key);
>> if (key.startsWith("unit:")) {
>> persistenceUnits.put(key.substring(5),"java:comp/env/"+value);
>> }
>> }
>> rc.getSingletons().add(new InjectableProvider<PersistenceUnit,
>> Type>() {
>> public ComponentScope getScope() {
>> return ComponentScope.Singleton;
>> }
>> public Injectable<EntityManagerFactory>
>> getInjectable(ComponentContext
>> ic, PersistenceUnit pu, Type c) {
>> if (!c.equals(EntityManagerFactory.class))
>> return null;
>> // TODO localize error message
>> if (!persistenceUnits.containsKey(pu.unitName()))
>> throw new ContainerException("Persistence unit '"+
>> pu.unitName()+
>> "' is not configured as a servlet parameter in web.xml");
>> String jndiName = persistenceUnits.get(pu.unitName());
>> ThreadLocalNamedInvoker<EntityManagerFactory> emfHandler =
>> new ThreadLocalNamedInvoker<EntityManagerFactory>(jndiName);
>> final EntityManagerFactory emf = (EntityManagerFactory)
>> Proxy.newProxyInstance(
>> this.getClass().getClassLoader(),
>> new Class[] { EntityManagerFactory.class },
>> emfHandler);
>> return new Injectable<EntityManagerFactory>() {
>> public EntityManagerFactory getValue() {
>> return emf;
>> }
>> };
>> }
>> });
>> }
>> }
>>
>>
>> On Oct 9, 2009, at 2:42 AM, Roan Brasil Monteiro wrote:
>>
>>> Hello ,
>>>
>>>
>>> I'd installed Glassfishv2.1 and configured to connect to Mysql in
>>> Resources and JNDI. After that I created on my eclipse using
>>> maven2eclipse plugin a jersey-quickstart-webapp archetype . I
>>> downloaded the org.hibernate libs (hibernate-core,
>>> hibernate-annotations, hibernate-entitymanger...) . I created a
>>> folder
>>> META-INF inside of src/main/resources and inside of META-INF my
>>> persistence.xml . I didn't put information like pass, user and url
>>> because I configured that on Glassfish. When I am running the
>>> glassfish with my project its running very well without hibernate/
>>> jpa.
>>> When I am running with it I am getting:
>>> I dont know what to do more how to fix it. I read the Paul answer
>>> but
>>> I couldn't resolve it.
>>> If anyone can help me, please let me know. How can I answer this
>>> topi?c->
>>> https://jersey.dev.java.net/servlets/ReadMsg?list=users&msgNo=8029&raw=true
>>> <https://jersey.dev.java.net/servlets/ReadMsg?list=users&msgNo=8029&raw=true
>>> >
>>> NullPointerException
>>>
>>> *_at_PersistenceUnit(name = "citespace-jpa")
>>> protected EntityManagerFactory emf;
>>>
>>> @GET
>>> @Produces("text/plain")
>>> public String getIt() {
>>>
>>> line 29--> EntityManager manager = emf.createEntityManager();
>>> Query q = manager.createQuery("SELECT c from Country c where c.id
>>> <http://c.id/>= :id ");
>>> q.setParameter("id", 1);
>>> List<Country> c = q.getResultList();*
>>> *.
>>> .
>>> .*
>>> *[#|2009-10-08T16:39:59.524-0300|SEVERE|sun-appserver2.1|
>>> com.sun.jersey.server.impl.application.WebApplicationImpl|
>>> _ThreadID
>>> =19;_ThreadName=httpSSLWorkerThread-8080-0;_RequestID=52a5f523-
>>> f63f-4473-8f43-fa33a831d784;|The
>>> RuntimeException could not be mapped to a response, re-throwing to
>>> the
>>> HTTP container
>>> java.lang.NullPointerException
>>> at com.citespace.citespacerest.Test.getIt(Test.java:29)
>>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native *Method)
>>> at sun.reflect.
>>> NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>>> at
>>> sun
>>> .reflect
>>> .DelegatingMethodAccessorImpl
>>> .invoke(DelegatingMethodAccessorImpl.java:25)
>>> at java.lang.reflect.Method.invoke(Method.java:597)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server
>>> .impl.model.method.dispatch.AbstractResourceMethodDispatchProvider
>>> $
>>> TypeOutInvoker
>>> ._dispatch(AbstractResourceMethodDispatchProvider.java:156)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server
>>> .impl
>>> .model
>>> .method
>>> .dispatch
>>> .ResourceJavaMethodDispatcher
>>> .dispatch(ResourceJavaMethodDispatcher.java:67)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:
>>> 208)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server
>>> .impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:75)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server
>>> .impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:115)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server
>>> .impl
>>> .uri
>>> .rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:
>>> 67)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server
>>> .impl
>>> .application
>>> .WebApplicationImpl._handleRequest(WebApplicationImpl.java:746)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server
>>> .impl
>>> .application
>>> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:711)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server
>>> .impl
>>> .application
>>> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:702)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .spi.container.servlet.WebComponent.service(WebComponent.java:340)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .spi
>>> .container.servlet.ServletContainer.service(ServletContainer.java:
>>> 452)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .spi
>>> .container.servlet.ServletContainer.service(ServletContainer.java:
>>> 633)
>>> at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
>>> at
>>> org
>>> .apache
>>> .catalina
>>> .core
>>> .ApplicationFilterChain.servletService(ApplicationFilterChain.java:
>>> 427)
>>> at
>>> org
>>> .apache
>>> .catalina
>>> .core.StandardWrapperValve.invoke(StandardWrapperValve.java:315)
>>> at
>>> org
>>> .apache
>>> .catalina
>>> .core
>>> .StandardContextValve.invokeInternal(StandardContextValve.java:287)
>>> at
>>> org
>>> .apache
>>> .catalina
>>> .core.StandardContextValve.invoke(StandardContextValve.java:218)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
>>> at com.sun.enterprise.web.WebPipeline.invoke
>>> <http://web.WebPipeline.invoke>(WebPipeline.java:94)
>>> at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke
>>> <http://
>>> web
>>> .PESessionLockingStandardPipeline
>>> .invoke>(PESessionLockingStandardPipeline.java:98)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardHostValve.invoke(StandardHostValve.java:222)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
>>> at
>>> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:
>>> 1096)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:
>>> 166)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
>>> at
>>> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:
>>> 1096)
>>> at
>>> org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:
>>> 288)
>>> at
>>> com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter
>>> <http://
>>> web
>>> .connector
>>> .grizzly
>>> .DefaultProcessorTask.invokeAdapter>(DefaultProcessorTask.java:647)
>>> at
>>> com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess
>>> <http://
>>> web
>>> .connector
>>> .grizzly.DefaultProcessorTask.doProcess>(DefaultProcessorTask.java:
>>> 579)
>>> at
>>> com
>>> .sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process
>>> <http://
>>> web
>>> .connector
>>> .grizzly.DefaultProcessorTask.process>(DefaultProcessorTask.java:
>>> 831)
>>> at
>>> com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask
>>> <http://
>>> web
>>> .connector
>>> .grizzly
>>> .DefaultReadTask.executeProcessorTask>(DefaultReadTask.java:341)
>>> at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask
>>> <http://
>>> web.connector.grizzly.DefaultReadTask.doTask>(DefaultReadTask.java:
>>> 263)
>>> at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask
>>> <http://
>>> web.connector.grizzly.DefaultReadTask.doTask>(DefaultReadTask.java:
>>> 214)
>>> at com.sun.enterprise.web.connector.grizzly.TaskBase.run
>>> <http://web.connector.grizzly.TaskBase.run>(TaskBase.java:265)
>>> at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run
>>> <http://
>>> web
>>> .connector.grizzly.ssl.SSLWorkerThread.run>(SSLWorkerThread.java:
>>> 106)
>>> |#]
>>>
>>> [#|2009-10-08T16:39:59.526-0300|SEVERE|sun-appserver2.1|
>>> javax.enterprise.system.container.web|
>>> _ThreadID
>>> =19;_ThreadName=httpSSLWorkerThread-8080-0;_RequestID=52a5f523-
>>> f63f-4473-8f43-fa33a831d784;|StandardWrapperValve[Jersey
>>> Web Application]: PWC1406: Servlet.service() for servlet Jersey Web
>>> Application threw exception
>>> java.lang.NullPointerException
>>> at com.citespace.citespacerest.Test.getIt(Test.java:29)
>>> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>> at
>>> sun
>>> .reflect
>>> .NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>>> at
>>> sun
>>> .reflect
>>> .DelegatingMethodAccessorImpl
>>> .invoke(DelegatingMethodAccessorImpl.java:25)
>>> at java.lang.reflect.Method.invoke(Method.java:597)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server
>>> .impl.model.method.dispatch.AbstractResourceMethodDispatchProvider
>>> $
>>> TypeOutInvoker
>>> ._dispatch(AbstractResourceMethodDispatchProvider.java:156)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server
>>> .impl
>>> .model
>>> .method
>>> .dispatch
>>> .ResourceJavaMethodDispatcher
>>> .dispatch(ResourceJavaMethodDispatcher.java:67)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:
>>> 208)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server
>>> .impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:75)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server
>>> .impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:115)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server
>>> .impl
>>> .uri
>>> .rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:
>>> 67)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server
>>> .impl
>>> .application
>>> .WebApplicationImpl._handleRequest(WebApplicationImpl.java:746)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server
>>> .impl
>>> .application
>>> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:711)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .server
>>> .impl
>>> .application
>>> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:702)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .spi.container.servlet.WebComponent.service(WebComponent.java:340)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .spi
>>> .container.servlet.ServletContainer.service(ServletContainer.java:
>>> 452)
>>> at
>>> com
>>> .sun
>>> .jersey
>>> .spi
>>> .container.servlet.ServletContainer.service(ServletContainer.java:
>>> 633)
>>> at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
>>> at
>>> org
>>> .apache
>>> .catalina
>>> .core
>>> .ApplicationFilterChain.servletService(ApplicationFilterChain.java:
>>> 427)
>>> at
>>> org
>>> .apache
>>> .catalina
>>> .core.StandardWrapperValve.invoke(StandardWrapperValve.java:315)
>>> at
>>> org
>>> .apache
>>> .catalina
>>> .core
>>> .StandardContextValve.invokeInternal(StandardContextValve.java:287)
>>> at
>>> org
>>> .apache
>>> .catalina
>>> .core.StandardContextValve.invoke(StandardContextValve.java:218)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
>>> at com.sun.enterprise.web.WebPipeline.invoke
>>> <http://web.WebPipeline.invoke>(WebPipeline.java:94)
>>> at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke
>>> <http://
>>> web
>>> .PESessionLockingStandardPipeline
>>> .invoke>(PESessionLockingStandardPipeline.java:98)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardHostValve.invoke(StandardHostValve.java:222)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
>>> at
>>> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:
>>> 1096)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:
>>> 166)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
>>> at
>>> org
>>> .apache
>>> .catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
>>> at
>>> org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:
>>> 1096)
>>> at
>>> org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:
>>> 288)
>>> at
>>> com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter
>>> <http://
>>> web
>>> .connector
>>> .grizzly
>>> .DefaultProcessorTask.invokeAdapter>(DefaultProcessorTask.java:647)
>>> at
>>> com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess
>>> <http://
>>> web
>>> .connector
>>> .grizzly.DefaultProcessorTask.doProcess>(DefaultProcessorTask.java:
>>> 579)
>>> at
>>> com
>>> .sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process
>>> <http://
>>> web
>>> .connector
>>> .grizzly.DefaultProcessorTask.process>(DefaultProcessorTask.java:
>>> 831)
>>> at
>>> com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask
>>> <http://
>>> web
>>> .connector
>>> .grizzly
>>> .DefaultReadTask.executeProcessorTask>(DefaultReadTask.java:341)
>>> at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask
>>> <http://
>>> web.connector.grizzly.DefaultReadTask.doTask>(DefaultReadTask.java:
>>> 263)
>>> at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask
>>> <http://
>>> web.connector.grizzly.DefaultReadTask.doTask>(DefaultReadTask.java:
>>> 214)
>>> at com.sun.enterprise.web.connector.grizzly.TaskBase.run
>>> <http://web.connector.grizzly.TaskBase.run>(TaskBase.java:265)
>>> at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run
>>> <http://
>>> web
>>> .connector.grizzly.ssl.SSLWorkerThread.run>(SSLWorkerThread.java:
>>> 106)
>>> |#]
>>>
>>>
>>>
>>> --
>>> Atenciosamente,
>>>
>>> Roan Brasil Monteiro
>>> http://roanbrasil.wordpress.com/
>>
>
>
> --
> Tarjei Huse
> Mobil: 920 63 413
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>