@PersistenceContext和@Autowired在EntityManager上应用的不同
qq_26257509:
他这个回答是不正确的...
2
I think @Autowire will work same way as @PersistenceContext
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.misc.jpa-context
When working with multiple EntityManager instances and custom repository implementations, you need to wire the correct EntityManager into the repository implementation class. You can do so by explicitly naming the EntityManager in the @PersistenceContext annotation or, if the EntityManager is @Autowired, by using @Qualifier.
As of Spring Data JPA 1.9, Spring Data JPA includes a class called JpaContext that lets you obtain the EntityManager by managed domain class, assuming it is managed by only one of the EntityManager instances in the application. The following example shows how to use JpaContext in a custom repository:
|