glassfish
  1. glassfish
  2. GLASSFISH-20817

Cannot inject entity manager into JAX-RS resources or providers

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Cannot Reproduce
    • Affects Version/s: 4.0_b89_RC5
    • Fix Version/s: None
    • Component/s: jax-rs
    • Labels:
      None
    • Environment:

      OSX 10.9, NetBeans 7.4 beta

      Description

      As the title says: I can no longer inject entity managers with @PersistenceContext.

      Simple text class:

      @Path("test")
      @Transactional
      public class TestResource
      {
      @PersistenceContext
      private EntityManager em;

      @GET
      public String testInject()

      { return "" + (em == null); // returns true }

      }

      with configuration class:

      @ApplicationPath("api")
      public class Configuration extends Application
      {

      }

      The project contains a persistence.xml with one persistence unit in it.

      I have to exact same problems in @Provider classes.

      And for some reason, I do not have it all the time. I have one older project where the injection works in resources classes, but not provider classes. In newer projects, nothing works.

        Activity

        Hide
        svanimpe added a comment -

        It seems like adding beans.xml to the project solved the problem of injecting into resource classes, but not provider classes.

        My test case classes:

        @Path("test")
        @Transactional
        public class TestResource
        {
        @GET
        @Produces(MediaType.APPLICATION_JSON)
        public TestObject testInject()

        { return new TestObject(); }

        }

        @Provider
        @Produces(MediaType.APPLICATION_JSON)
        public class TestProvider implements MessageBodyWriter<TestObject>
        {
        @PersistenceContext
        private EntityManager em;

        @Override
        public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)

        { return TestObject.class.isAssignableFrom(type); }

        @Override
        public long getSize(TestObject t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType)

        { return -1; }

        @Override
        public void writeTo(TestObject t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException
        {
        JsonObjectBuilder result = Json.createObjectBuilder();
        result.add("hasEntityManager", em != null);
        try (JsonWriter writer = Json.createWriter(entityStream))

        { writer.writeObject(result.build()); }

        }
        }

        TestObject is just an empty class.

        The resulting JSON is always:

        { "hasEntityManager": false }
        Show
        svanimpe added a comment - It seems like adding beans.xml to the project solved the problem of injecting into resource classes, but not provider classes. My test case classes: @Path("test") @Transactional public class TestResource { @GET @Produces(MediaType.APPLICATION_JSON) public TestObject testInject() { return new TestObject(); } } @Provider @Produces(MediaType.APPLICATION_JSON) public class TestProvider implements MessageBodyWriter<TestObject> { @PersistenceContext private EntityManager em; @Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return TestObject.class.isAssignableFrom(type); } @Override public long getSize(TestObject t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return -1; } @Override public void writeTo(TestObject t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { JsonObjectBuilder result = Json.createObjectBuilder(); result.add("hasEntityManager", em != null); try (JsonWriter writer = Json.createWriter(entityStream)) { writer.writeObject(result.build()); } } } TestObject is just an empty class. The resulting JSON is always: { "hasEntityManager": false }
        Hide
        svanimpe added a comment -

        The same test works fine on WildFly8, so if this is not a bug, please consider this a feature request.

        Show
        svanimpe added a comment - The same test works fine on WildFly8, so if this is not a bug, please consider this a feature request.
        Hide
        Jakub Podlesak added a comment -

        attached test-case application zip archive

        Show
        Jakub Podlesak added a comment - attached test-case application zip archive
        Hide
        Jakub Podlesak added a comment -

        Attached test case application, where i could inject EM into JAX-RS resource/provider just fine.

        Resource injection is being tested with the tests included, provider injection could be tested via
        http://localhost:8080/jersey-2137/test
        (which returns: {{

        {"entityManager":true}

        }})

        to run the tests:

        unzip jersey-2137.zip
        cd jersey-2137
        mvn clean package
        $AS_HOME/bin/asadmin start-domain
        $AS_HOME/bin/asadmin start-database
        $AS_HOME/bin/deploy target/*.war
        mvn -DskipTests=false test
        
        Show
        Jakub Podlesak added a comment - Attached test case application, where i could inject EM into JAX-RS resource/provider just fine. Resource injection is being tested with the tests included, provider injection could be tested via http://localhost:8080/jersey-2137/test (which returns: {{ {"entityManager":true} }}) to run the tests: unzip jersey-2137.zip cd jersey-2137 mvn clean package $AS_HOME/bin/asadmin start-domain $AS_HOME/bin/asadmin start-database $AS_HOME/bin/deploy target/*.war mvn -DskipTests= false test
        Hide
        svanimpe added a comment -

        I am unable to run the test case: Non-resolvable parent POM: Could not find artifact org.glassfish.jersey.tests.integration:project:pom:2.10-SNAPSHOT and 'parent.relativePath' points at wrong local POM @ line 45, column 13

        But even if it works for you, that doesn't help me any further I have had this problem for almost a year now, and so have all my students. I have reproduced this on over 20 machines by simply creating a new Java Web Application in NetBeans (7.4 and 8) with the classes in the first comment, adding an Application class, beans.xml and persistence.xml, and deploying it to the bundled version of GlassFish. Could you give that a try too?

        Show
        svanimpe added a comment - I am unable to run the test case: Non-resolvable parent POM: Could not find artifact org.glassfish.jersey.tests.integration:project:pom:2.10-SNAPSHOT and 'parent.relativePath' points at wrong local POM @ line 45, column 13 But even if it works for you, that doesn't help me any further I have had this problem for almost a year now, and so have all my students. I have reproduced this on over 20 machines by simply creating a new Java Web Application in NetBeans (7.4 and 8) with the classes in the first comment, adding an Application class, beans.xml and persistence.xml, and deploying it to the bundled version of GlassFish. Could you give that a try too?
        Hide
        Jakub Podlesak added a comment -

        Could you please attach your reproducible test case application so that i can give it a try? Thanks!

        Show
        Jakub Podlesak added a comment - Could you please attach your reproducible test case application so that i can give it a try? Thanks!
        Hide
        svanimpe added a comment -

        This problem still exists on 4.1. @Jakub: my reproducible test case are simply the classes I listed in my first comment.

        Show
        svanimpe added a comment - This problem still exists on 4.1. @Jakub: my reproducible test case are simply the classes I listed in my first comment.

          People

          • Assignee:
            Jakub Podlesak
            Reporter:
            svanimpe
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: