3 Replies Latest reply on Aug 27, 2011 9:42 AM by pedrokowalski

    EntityManager in EJB not injected in overloaded method

    pedrokowalski

      Howdy Guys!

       

      I'm facing a problem with injection of an EntityManager in my EJB.

      It works fine when there are no overloaded methods, but as soon as I add such method, the EntityManager within the method results in 'null'.

      I've provided a test project which I attach to this post - below you can see the class under test (simplified for the sake of brevity):

       

      package com.test;
      
      import java.io.Serializable;
      
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      
      public class CRUDBean<K extends Serializable, T extends Serializable> {
      
           @PersistenceContext
           private EntityManager em;
      
           public T create(T entity) {
                // This results to properly injected EM.
                System.out.println("!!!! Create EM: " + em);          
                return null;
           }
      
           public Object find(K id, Class<T> entityClass) {
                return null;
           }
      
           public Object find(String parameters, Class<T> entityClass) {
                // Results to null. When the above (overloaded) method is removed, it results to properly injected EM.
                System.out.println("!!!! Find EM: " + em);               
      
                return null;
           }
      }
      

       

      I've tested this case using Glassfish 3.1 embedded and remote. In both cases the 'em' in find method is null.

       

      Franky, I'm not sure if this is an arquillian problem, as I don't use the @PersistenceContext directly from the test class. It seems it's more like a glassfish one but because I'm not sure about that and because I'm using arquillian to test this case, I'm posting it here.

       

      Thanks in advance for your help!

       

      Cheers!

        • 1. Re: EntityManager in EJB not injected in overloaded method
          aslak

          I suspect this is a GlassFish issue. Arquillian is only involed in looking up the Bean, not creating it.

          1 of 1 people found this helpful
          • 2. Re: EntityManager in EJB not injected in overloaded method
            pedrokowalski

            Yup, it isn't an arquillian problem.

             

            The above (as well as the below) code works fine on JBoss AS7.

            In the matter of fact the problem occurs when you're using generics, so the following example can be simplified to the following form:

             

            @Stateless
            public class CRUDBean<T> {
            
                @PersistenceContext
                private EntityManager em;
            
                public void find(Object e) {
                    System.out.println("!!!!!!!!!!!!!!! find(Object e) em: " + em);
                }
            
                public void find(T id) {
                    System.out.println("!!!!!!!!!!!!!!! find(T id) em: " + em);
                }
            }
            

             

            Then:

            1. find(Object e) will have the em injected properly.

            2. find(T id) will not have the em injected.

             

            Interestingly:

            - if you remove the arguments from find(Object e), both find methods will have em injected properly,

            - if you will change the signature of find(T id) to find(String id), both find methods will have em injected properly (the String class is just an example of non-generic used as an argument).

             

            I just posted it here, because it was so friggin' hidden thing that caused me a day of wondering why my tests were failing...

             

            Cheers!

             

            Message was edited by: Pedro Kowalski (java syntax highlighter and further simplification (no extends in generics)

            • 3. Re: EntityManager in EJB not injected in overloaded method
              pedrokowalski

              Just for information purposes - following issue has been created in glassfish JIRA: http://java.net/jira/browse/GLASSFISH-17235

               

              Cheers