users@jpa-spec.java.net

[jpa-spec users] Proposal: allow @PersistenceContext/@PersistenceUnit on method parameters

From: Oliver Gierke <ogierke_at_vmware.com>
Date: Sun, 13 Nov 2011 04:29:19 -0800

Hi all,

I'd like to propose to allow @PersistenceContext/_at_PersistenceConstructor on constructor parameters to be able to build immutable clients for it. As it's currently only allowed to use these annotations you usually end up with something like this:

class MyClient {

  @PersistenceContext
  private EntityManager em;
  private final MyDependency dep;

  @Inject
  public MyClient(MyDependency dep) {
    Assert.notNull(dep);
    this.dep = dep;
  }
}

so the class is half immutable in the sense of me having to rely on the infrastructure to inject the EntityManager via reflection or introducing an additional constructor to instantiate the class with MyDependency *and* EntityManager. So what I actually would like to be able to write is this:

class MyClient {

  private final EntityManager em;
  private final MyDependency dep;

  @Inject
  public MyClient(MyDependency dep, @PersistenceContext EntityManager em) {
    Assert.notNull(dep);
    Assert.notNull(em);
    this.dep = dep;
    this.em = em;
  }
}

As the container has to hold an EntityManager as injectable resource anyway it could already pipe it into the client on object construction instead of applying it later on.

Regards,
Ollie

-- 
/**
 * @author Oliver Gierke - Senior Member Technical Staff
 *
 * @param email ogierke_at_vmware.com
 * @param phone +49-351-30929001
 * @param fax   +49-351-418898439
 * @param skype einsdreizehn
 * @see http://www.olivergierke.de
 */