persistence icon indicating copy to clipboard operation
persistence copied to clipboard

Allow @PersistenceContext to be used on parameters ot enable constructor injection of EntityManagers

Open lukasj opened this issue 11 years ago • 5 comments

Currently it's not possible to inject EntityManagers into constructors as @PersistenceContext is defined to not be allowed on parameters. it would be cool to enable this as users could then design application components using constructor injection only.

lukasj avatar Feb 07 '14 11:02 lukasj

  • Issue Imported From: https://github.com/javaee/jpa-spec/issues/72
  • Original Issue Raised By:@glassfishrobot
  • Original Issue Assigned To: Unassigned

lukasj avatar Aug 31 '18 16:08 lukasj

@glassfishrobot Commented Reported by oliver.gierke

lukasj avatar Feb 07 '14 11:02 lukasj

@glassfishrobot Commented @arjantijms said: Wouldn't this automatically be possible when those contexts can be injected with @Inject?

lukasj avatar Mar 25 '14 19:03 lukasj

@glassfishrobot Commented This issue was imported from java.net JIRA JPA_SPEC-72

lukasj avatar May 05 '17 06:05 lukasj

This seems a small and sensible change. Perhaps it could be accommodated in a future, even if minor, release?

Reza Rahman Jakarta EE Ambassador, Author, Blogger, Speaker

Please note views expressed here are my own as an individual community member and do not reflect the views of my employer.

m-reza-rahman avatar Apr 28 '21 00:04 m-reza-rahman

So @PersistenceContext / @PeristenceUnit injection is defined at the same level as @Resource and @EJB, and perhaps some others I forget about after all these years. These other annotations don't support constructor/method injection either, so I don't see it as very natural that @PersistenceContext would.

Instead, if you need constructor/method injection, you should use CDI which has always supported this. Assuming that #460 goes through, and provides a slightly easier way to register a persistence unit as injectable via CDI, I would say that this issue is no longer relevant, and can be closed.

gavinking avatar Aug 15 '23 17:08 gavinking