4 Replies Latest reply on Sep 1, 2010 4:33 PM by Ricardo Campos

    Possible bugs in JBoss 6 M4: produced entity beans & injection in webservices

    Ricardo Campos Newbie

      Hello all!


      I'm having two issues with JBoss 6 M4 and CDI.


      1st Issue: Produced entity bean unknown to EntityManager



      It seems that the EntityManager doesn't recognize an entity bean if it comes from a producer method. Maybe it has something to do with its Javassist tranformation.


      Source



      package issues.issue1;
      
      import java.io.Serializable;
      
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      
      @Entity
      public class TestEntity implements Serializable {
      
        private Long id;
        private String testValue;
      
        @Id
        @GeneratedValue
        public Long getId() {
          return id;
        }
      
        public void setId(Long id) {
          this.id = id;
        }
      
        public String getTestValue() {
          return testValue;
        }
      
        public void setTestValue(String testValue) {
          this.testValue = testValue;
        }
      
      }
      



      package issues.issue1;
      
      import javax.ejb.Stateless;
      import javax.enterprise.context.RequestScoped;
      import javax.enterprise.inject.Produces;
      import javax.inject.Named;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      
      @Stateless
      public class TestProducer {
      
        @Produces
        @TestDatabase
        @PersistenceContext
        public static EntityManager entityManager;
      
        @Produces
        @ProducedEntity
        @RequestScoped
        @Named
        public TestEntity getTestEntity() {
          return new TestEntity();
        }
      
      }
      



      package issues.issue1;
      
      import javax.ejb.Stateful;
      import javax.enterprise.context.RequestScoped;
      import javax.inject.Inject;
      import javax.inject.Named;
      import javax.persistence.EntityManager;
      
      @Stateful
      @RequestScoped
      @Named
      public class TestAction {
      
        @Inject
        @TestDatabase
        private EntityManager entityManager;
      
        @Inject
        @ProducedEntity
        private TestEntity testEntity;
      
        public void testPersistence() {
          entityManager.persist(testEntity);
        }
      
      }
      



      qualifiers


      package issues.issue1;
      
      import java.lang.annotation.ElementType;
      import java.lang.annotation.Retention;
      import java.lang.annotation.RetentionPolicy;
      import java.lang.annotation.Target;
      
      import javax.inject.Qualifier;
      
      @Qualifier
      @Retention(RetentionPolicy.RUNTIME)
      @Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
      public @interface TestDatabase {}
      



      package issues.issue1;
      
      import java.lang.annotation.ElementType;
      import java.lang.annotation.Retention;
      import java.lang.annotation.RetentionPolicy;
      import java.lang.annotation.Target;
      
      import javax.inject.Qualifier;
      
      @Qualifier
      @Retention(RetentionPolicy.RUNTIME)
      @Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
      public @interface ProducedEntity {}
      



      index.xhtml


      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets">
      <h:head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
       <title>Issues</title>
      </h:head>
      <h:body>
       <h:messages />
       <h:form>
        <h:outputLabel value="Value" />
        <h:inputText value="#{testEntity.testValue}" />
        <h:commandButton value="Test" action="#{testAction.testPersistence}" />
       </h:form>
      </h:body>
      </html>
      



      Result



      Stacktrace when #{testAction.testPersistence} is executed:


      Caused by: java.lang.IllegalArgumentException: Unknown entity: issues.issue1.TestEntity_$$_javassist_26
           at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:671) [:3.5.2-Final]
           at org.jboss.jpa.impl.tx.TransactionScopedEntityManager.persist(TransactionScopedEntityManager.java:206) [:2.0.0]
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_16]
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_16]
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_16]
           at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_16]
           at org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:304) [:1.0.1.SP4]
           at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:54) [:1.0.1.SP4]
           at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:163) [:1.0.1.SP4]
           at org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:298) [:1.0.1.SP4]
           at org.jboss.weld.bean.builtin.CallableMethodHandler.invoke(CallableMethodHandler.java:64) [:1.0.1.SP4]
           at org.jboss.weld.util.CleanableMethodHandler.invoke(CleanableMethodHandler.java:43) [:1.0.1.SP4]
           at javax.persistence.EntityManager_$$_javassist_28.persist(EntityManager_$$_javassist_28.java)
           at issues.issue1.TestAction.testPersistence(TestAction.java:23) [:]
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_16]
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_16]
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_16]
           at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_16]
           at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122) [jboss-aop.jar:2.2.1.Alpha3]
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:111) [jboss-aop.jar:2.2.1.Alpha3]
           at org.jboss.ejb3.interceptors.container.ContainerMethodInvocationWrapper.invokeNext(ContainerMethodInvocationWrapper.java:72) [:1.0.7]
           at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor$InvocationContext.proceed(InvocationContextInterceptor.java:138) [:1.0.7]
           at org.jboss.weld.integration.ejb.interceptor.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:143) [:6.0.0.20100721-M4]
           at org.jboss.weld.integration.ejb.interceptor.Jsr299BindingsInterceptor.doAroundInvoke(Jsr299BindingsInterceptor.java:116) [:6.0.0.20100721-M4]
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_16]
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_16]
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_16]
           at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_16]
           at org.jboss.ejb3.interceptors.aop.EJB3InterceptorInterceptor.invoke(EJB3InterceptorInterceptor.java:83) [:1.0.7]
           at org.jboss.ejb3.interceptors.aop.EJB3InterceptorInterceptor.invoke(EJB3InterceptorInterceptor.java:70) [:1.0.7]
           at org.jboss.ejb3.interceptors.container.ContainerMethodInvocationWrapper.invokeNext(ContainerMethodInvocationWrapper.java:62) [:1.0.7]
           at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor$InvocationContext.proceed(InvocationContextInterceptor.java:138) [:1.0.7]
           at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:47) [:1.0.1.SP4]
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_16]
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_16]
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_16]
           at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_16]
           at org.jboss.ejb3.interceptors.aop.EJB3InterceptorInterceptor.invoke(EJB3InterceptorInterceptor.java:83) [:1.0.7]
           at org.jboss.ejb3.interceptors.aop.EJB3InterceptorInterceptor.invoke(EJB3InterceptorInterceptor.java:70) [:1.0.7]
           at org.jboss.ejb3.interceptors.container.ContainerMethodInvocationWrapper.invokeNext(ContainerMethodInvocationWrapper.java:62) [:1.0.7]
           at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.invoke(InterceptorSequencer.java:76) [:1.0.7]
           at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.aroundInvoke(InterceptorSequencer.java:62) [:1.0.7]
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_16]
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_16]
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_16]
           at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_16]
           at org.jboss.aop.advice.PerJoinpointAdvice.invoke(PerJoinpointAdvice.java:174) [jboss-aop.jar:2.2.1.Alpha3]
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.Alpha3]
           at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.fillMethod(InvocationContextInterceptor.java:72) [:1.0.7]
           at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_fillMethod_7946314.invoke(InvocationContextInterceptor_z_fillMethod_7946314.java) [:]
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.Alpha3]
           at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.setup(InvocationContextInterceptor.java:88) [:1.0.7]
           at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_setup_7946314.invoke(InvocationContextInterceptor_z_setup_7946314.java) [:]
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.Alpha3]
           at org.jboss.ejb3.async.impl.interceptor.FutureSerializingInterceptor.invoke(FutureSerializingInterceptor.java:88) [:1.0.0-alpha-5]
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.Alpha3]
           at org.jboss.ejb3.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:62) [:1.5.0-alpha-4]
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.Alpha3]
           at org.jboss.ejb3.entity.ExtendedPersistenceContextPropagationInterceptor.invoke(ExtendedPersistenceContextPropagationInterceptor.java:60) [:1.5.0-alpha-4]
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.Alpha3]
           at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:56) [:1.5.0-alpha-4]
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.Alpha3]
           at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47) [:1.5.0-alpha-4]
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.Alpha3]
           at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42) [:1.0.3]
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.Alpha3]
           at org.jboss.ejb3.stateful.StatefulInstanceInterceptor.invoke(StatefulInstanceInterceptor.java:81) [:1.5.0-alpha-4]
           at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102) [jboss-aop.jar:2.2.1.Alpha3]
           at org.jboss.ejb3.core.context.SessionInvocationContextAdapter.proceed(SessionInvocationContextAdapter.java:90) [:1.5.0-alpha-4]
           at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:247) [:0.0.1]
           ... 92 more
      



      2nd Issue: Injection doesn't work in WebServices



      It seems weld is not injecting in WebService classes.


      Source



      package issues.issue2;
      
      import javax.ejb.Stateless;
      
      @Stateless
      public class InjectionTest {}
      



      package issues.issue2;
      
      import javax.ejb.Stateless;
      import javax.inject.Inject;
      import javax.jws.Oneway;
      import javax.jws.WebMethod;
      import javax.jws.WebService;
      
      @Stateless
      @WebService
      public class IssueWS {
      
        @Inject
        private InjectionTest injectionTest;
      
        @WebMethod
        @Oneway
        public void testing(String issue) {
          System.out.println(injectionTest);
        }
      
      }
      



      Result



      Output from IssuWS.testing() when called through a webservice client:


      14:14:22,118 INFO  [STDOUT] null
      



      Configuration



      web.xml


      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
      
       <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
       </context-param>
      
       <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
       </servlet>
      
       <servlet>
        <servlet-name>Issue WebService</servlet-name>
        <servlet-class>issues.issue2.IssueWS</servlet-class>
       </servlet>
      
       <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
       </servlet-mapping>
      
       <servlet-mapping>
        <servlet-name>Issue WebService</servlet-name>
        <url-pattern>/ws</url-pattern>
       </servlet-mapping>
      
       <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
       </welcome-file-list>
      
       <session-config>
        <session-timeout>30</session-timeout>
       </session-config>
      
      </web-app>