Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injecting EntityManagers through constructor injection (and at non-@PersistenceContext injection points in general) [SPR-10443] #15076

Closed
spring-projects-issues opened this issue Apr 5, 2013 · 8 comments
Assignees
Labels
has: votes-jira Issues migrated from JIRA with more than 10 votes at the time of import in: data Issues in data modules (jdbc, orm, oxm, tx) type: documentation A documentation task
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Oliver Drotbohm opened SPR-10443 and commented

Currently you can inject any Spring bean into clients through constructor injection. A JPA EntityManager is an exception to that. Constructor injection has the benefit of being able to design classes in an immutable way. Also, you communicate required dependencies through it easily. Sadly, @PersistenceContext cannot be used on a constructor parameter and this probably won't change befor JPA 2.2 (giving 2.1 is close to final).

It would be cool to simply be able to inject EntityManager instances through either @Autowired or @Inject.


Issue Links:

  • DATAJPA-445 Enable support to inject EntityManager via constructor

12 votes, 17 watchers

@spring-projects-issues
Copy link
Collaborator Author

Oliver Drotbohm commented

Created a ticket in Spring Data JPA to locally implement the support in there as it should be easy to add to the already registered bean definitions.

@spring-projects-issues
Copy link
Collaborator Author

Oliver Drotbohm commented

We've implemented the support in Spring Data JPA and it seems generally useable even in a non Spring Data context. Activation of annotation based configuration could register the BeanFactoryPostProcessor as well.

@spring-projects-issues
Copy link
Collaborator Author

Marcel Overdijk commented

To bad this is not possible yet. Especially in the light of http://olivergierke.de/2013/11/why-field-injection-is-evil/

@spring-projects-issues
Copy link
Collaborator Author

Oliver Drotbohm commented

Marcel Overdijk – Care to elaborate why the Spring Data JPA support for that is insufficient?

@spring-projects-issues
Copy link
Collaborator Author

Marcel Overdijk commented

Oliver Drotbohm I have a project not using Spring Data. In a earlier post you mentioned support in Spring Data JPA which seems generally usable even in a non Spring Data context. What do you mean exactly with that?

@spring-projects-issues
Copy link
Collaborator Author

Francisco Lozano commented

I also want to use constructor-injection of EntityManager without Spring Data JPA, feels weird that field injection is forced here. An @Autowired constructor should work... 

@spring-projects-issues spring-projects-issues added in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement has: votes-jira Issues migrated from JIRA with more than 10 votes at the time of import labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.x Backlog milestone Jan 11, 2019
csieflyman added a commit to csieflyman/springboot-limado that referenced this issue Apr 3, 2019
@lucasoares
Copy link

Will this be fixed anytime soon?

@jhoeller
Copy link
Contributor

jhoeller commented Jul 13, 2023

For the time being, a plain Spring application can define a bean of type org.springframework.orm.jpa.support.SharedEntityManagerBean, wiring it to a specific EntityManagerFactory, potentially defining a qualifier on it (e.g. the persistence unit name) - and then injecting it via @Autowired and the common qualifier mechanism if necessary, or simply through a plain constructor argument.

Due to the separate nature of the shared EntityManager instance, there is no first-class mechanism to do so implicitly. An explicit SharedEntityManagerBean is a straightforward FactoryBean matched by type and does not require additional post-processors. Alternatively, you could declare an @Bean method which returns an EntityManager built through SharedEntityManagerCreator.createSharedEntityManager, not even involving a FactoryBean then:

@Bean
public EntityManager sharedEntityManager(EntityManagerFactory emf) {
    return SharedEntityManagerCreator.createSharedEntityManager(emf);
}

That's essentially what Spring Data JPA's post-processor ends up registering as well. If there is nothing else we can do at the core Spring Framework level, maybe we should explicitly show the approach above in the reference documentation.

@jhoeller jhoeller self-assigned this Jul 18, 2023
@jhoeller jhoeller added type: documentation A documentation task and removed type: enhancement A general enhancement labels Jul 18, 2023
@jhoeller jhoeller modified the milestones: 6.x Backlog, 6.1.0-M3, 6.0.12 Jul 18, 2023
@jhoeller jhoeller changed the title Allow injecting EntityManagers through constructor injection (and at non-@PersistenceContext injection points in general) [SPR-10443] Injecting EntityManagers through constructor injection (and at non-@PersistenceContext injection points in general) [SPR-10443] Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has: votes-jira Issues migrated from JIRA with more than 10 votes at the time of import in: data Issues in data modules (jdbc, orm, oxm, tx) type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

3 participants