7 Replies Latest reply on Apr 11, 2013 2:15 PM by bmajsak

    @PersistenceContext is not injected..

    klind

      Hi, trying to inject the

       

      @PersistenceContext

      private EntityManager em;

       

      But is is null..

       

       

      @PersistenceContext 
                private EntityManager em;
      
                @Inject 
                private UserTransaction utx;
      
      
                @Deployment
                public static JavaArchive createDeployment() {
                          JavaArchive jar = ShrinkWrap.create(JavaArchive.class)//
                                              .addClass(PrintCustomerDao.class) //
                                              .addClass(OrderDao.class) //
                                              .addAsManifestResource("persistence.xml")
                                              .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
                          System.out.println(jar.toString(true));
                          return jar;
                }
      
      
                @Before
                public void setUp() {
                       printCustomer = printCustomerDao.getVendor(PrintCustomerDaoTest.PRINT_CUSTOMER_ID, em);
                }
      
      

       

      in the setup method em is null.

      I tried different approaches to ad the persistence.xml, which is located in /src/test/resources/META-INF

       

      the system.out.println gives..

       

      /com/
      /com/farheap/
      /com/farheap/jsi/
      /com/farheap/jsi/dao/
      /com/farheap/jsi/dao/OrderDao.class
      /com/farheap/jsi/dao/PrintCustomerDao.class
      /META-INF/
      /META-INF/persistence.xml
      /META-INF/beans.xml
      
        • 1. Re: @PersistenceContext is not injected..
          bmajsak

          Which container are you using? Can you also share relevant bits of POM so we can see which exact versions of Arquillian toolset are involved?

          • 2. Re: @PersistenceContext is not injected..
            klind

            Here is the arquillian part...

             

            <dependency>
                      <groupId>org.jboss.arquillian.junit</groupId>
                      <artifactId>arquillian-junit-container</artifactId>
                      <version>1.0.3.Final</version>
                      <scope>test</scope>
            </dependency>
            <dependency>
                      <groupId>org.jboss.arquillian.container</groupId>
                      <artifactId>arquillian-weld-ee-embedded-1.1</artifactId>
                      <version>1.0.0.CR6</version>
                      <scope>test</scope>
            </dependency>
            <dependency>
                      <groupId>org.jboss.weld</groupId>
                      <artifactId>weld-core</artifactId>
                      <version>1.1.11.Final</version>
                      <scope>test</scope>
            </dependency>
            
            • 3. Re: @PersistenceContext is not injected..
              bmajsak

              Ok, that's the problem. Weld is not fully Java EE environment, therefore @PersistenceContext is not supported, have a look here.

              • 4. Re: @PersistenceContext is not injected..
                klind

                ahhh..

                 

                so I will have to do this :

                 

                 

                private EntityManager em;
                
                          @Inject
                          private UserTransaction utx;
                
                          @Deployment
                          public static JavaArchive createDeployment() {
                                    JavaArchive jar = ShrinkWrap.create(JavaArchive.class)//
                                                        .addClass(PrintCustomerDao.class)//
                                                        .addClass(OrderDao.class)//
                                                        .addAsManifestResource("persistence.xml")
                                                        .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
                                    System.out.println(jar.toString(true));
                                    return jar;
                          }
                
                          @Before
                          public void setUp() {
                                    EntityManagerFactory f = Persistence.createEntityManagerFactory("jsi-test");
                                    em = f.createEntityManager();
                          }
                
                
                • 5. Re: @PersistenceContext is not injected..
                  bmajsak

                  Not sure if UserTransaction will be available though Why not some normal container instead?  JBoss managed is ain't slow

                  • 6. Re: @PersistenceContext is not injected..
                    klind

                    ya the UserTransaction was some leftover..

                     

                    How would the setup look like with a JBoss managed container ?

                    • 7. Re: @PersistenceContext is not injected..
                      bmajsak

                      Please have a look at the guides we crafted http://arquillian.org/guides/

                      You will find it right away