Hi all,
currently @PersistenceContext and @PersistenceUnit can not be used with constructor parameters which essentially prevents clients from being designed in an immutable way. Is there a dedicated reason this is the case? I propose to add ElementType.PARAMETER to the array of @Target for both annotations so that it can be used as follows:
@Named
class MyComponent {
private final MyDependency dependency;
private final EntityManager em;
@Inject
public MyComponent(@PersistenceContext EntityManager em, MyDependency dependency) {
// null checks to fail fast
this.em = em;
this.dependency = dependency;
}
}
Especially if you're used to using constructor arguments for mandatory dependencies an EntityManager is always the odd one out, as it cannot be assigned to a final field, not handed into a constructor etc.
Essentially we could allow @PersistenceContext and @PersistenceUnit be entirely optional as the identification can be done through the type. The additional annotation would then only be necessary in case you actually want to configure something special.
Thoughts?
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
*/