users@jpa-spec.java.net

[jpa-spec users] [jsr338-experts] Re: Improved injection capabilities for EntityManager(Factory)

From: Steve Ebersole <steve.ebersole_at_redhat.com>
Date: Wed, 18 Jul 2012 21:45:56 -0500

Cant speak to the container implementation side of this in terms of
feasibility. But I think it absolutely makes sense from a user
perspective.

On Wed 18 Jul 2012 07:02:51 AM CDT, Oliver Gierke wrote:
> 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
>