0

I am wondering from where does come this entityManager via constructor? Why i do not have to use @PersistenceContext annotation below? Spring creates this bean via @Repository annotation.

@Repository
public class RRepositoryCustomImpl {
    private final EntityManager entityManager;

    @Autowired
    public RRepositoryCustomImpl(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
}

Second option is using @PersistenceContext annotation:

@Repository
public class ACustomRepository {

    @PersistenceContext
    private EntityManager entityManager;
}

Why we can define EntityManager without @PersistenceContext annotation in the code example above? What's the difference between this two approaches? From where Spring takes EntityManager in this two scenarios?

Possible duplicate of: @Autowired vs @PersistenceContext for EntityManager bean

tryingHard
  • 1,794
  • 4
  • 35
  • 74
  • What's your question? – Ori Marko Aug 27 '19 at 08:52
  • Why we can define EntityManager without `@PersistenceContext` annotation in the code example above? What's the difference between this two approaches? From where Spring takes EntityManager in this two scenarios? – tryingHard Aug 27 '19 at 08:57

1 Answers1

0

the entityManager is marked as Autowired in the constructor, because of that you are able to use it.

alexrn
  • 41
  • 6
  • Why we can define EntityManager without @PersistenceContext annotation in the code example above? What's the difference between this two approaches? From where Spring takes EntityManager in this two scenarios? – tryingHard Aug 27 '19 at 08:57
  • 1
    You can check this post: https://stackoverflow.com/questions/31335211/autowired-vs-persistencecontext-for-entitymanager-bean – alexrn Aug 27 '19 at 08:59