2 Replies Latest reply on Jan 7, 2009 12:53 PM by wolfc

    Looking up a PU from @PersistenceContext annotation

    pmuir

      I have a field/method annotated @PersistenceContext (not on an EJB) that I need to inject. I have full access to the field/method (declared type, annotation), and need a way to get the correct persistence unit from JBoss EJB3. This could be directly (return the correct object) or a JNDI reference.

        • 1. Re: Looking up a PU from @PersistenceContext annotation
          pmuir

          Also, I have full access to MC at this point, so what I'm doing for @EJB injection is to lookup the EjbReferenceResolver MC bean and use it to get the JNDI name for the injection point.

          • 2. Re: Looking up a PU from @PersistenceContext annotation
            wolfc

            After injecting PersistenceUnitDependencyResolver, this piece of pseudo code should do the trick:

            String beanName = persistenceUnitDependencyResolver.resolvePersistenceUnitSupplier(deploymentUnit, persistenceUnitName);
            PersistenceUnitDeployment deployment = lookup(beanName);
            ManagedEntityManagerFactory managedFactory = deployment.getManagedFactory();
            return new InjectedEntityManagerFactory(managedFactory);

            Although PersistenceUnitDeployment is an internal. Maybe we should add a simple utility class to jpa-deployers.