4 Replies Latest reply on Oct 4, 2012 8:53 AM by grossetieg

    Weld proxy returns EntityManagerFactoryImpl / TransactionScopedEntityManager

    grossetieg

      Hi,

       

      I have several persistence units and I want to resolve it at runtime. I produce EntityManager with the @Produces annotation :

       

      @Singleton
      public class JPAResourceProducer {
      
      
           @Produces
           @PersistenceUnit(unitName="foo-bar-ds")
           @PersistenceUnitQualifier(packagePrefix = "com.foo.bar")
           static EntityManager foorBarPersistenceUnit;
      }
      

       

      Then I inject any EntityManager :

       

      @Singleton
      public class JPAResourceInstance {
      
           @Inject
           @Any
           private Instance<EntityManager> entityManagerInstance;
      
           public void soSomeWork() {
                for (EntityManager entityManager : entityManagerInstance) {
                     // do some work...
           }
      }
      

       

      When I want to call some method on the entityManager an exception is thrown :

       

      Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_26]
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [rt.jar:1.6.0_26]
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [rt.jar:1.6.0_26]
                at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_26]
                at org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:264) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
                at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:52) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
                at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:137) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
                at org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:260) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
                at org.jboss.weld.bean.builtin.CallableMethodHandler.invoke(CallableMethodHandler.java:51) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
                at org.jboss.weld.bean.proxy.EnterpriseTargetBeanInstance.invoke(EnterpriseTargetBeanInstance.java:56) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
                at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:105) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
                at org.jboss.weldx.persistence.EntityManager$-1808529401$Proxy$_$$_Weld$Proxy$.getEntityManagerFactory(EntityManager$-1808529401$Proxy$_$$_Weld$Proxy$.java) [hibernate-jpa-2.0-api-1.0.1.Final.jar:]
      

       

      With some debug I found that some of the produced EntityManager are "org.hibernate.ejb.EntityManagerFactoryImpl" and some are "org.jboss.as.jpa.container.TransactionScopedEntityManager". When I call method "on" TransactionScopedEntityManager the exception is not thrown.

      weld_proxy.png

       

      I didn't find any difference between my persistence.xml or my JPAResourceProducer classes. What I'm missing ?

      I'm using weld 1.1.5.AS71.Final with AS7.1.1.Final.

       

      Your help will be greatly appreciated !

       

       

      Guillaume.