Spring boot Persistence Context annotation

11,645

Solution 1

In the example itself they have explained the concept behind using @PersistenceContext :

@PersistenceContext – We need to understand how we are able to connect with the database using just simple annotation @PersistenceContext and what it is.

  1. Entities are managed by javax.persistence.EntityManager instance using persistence context.
  2. Each EntityManager instance is associated with a persistence context.
  3. Within the persistence context, the entity instances and their lifecycle are managed.
  4. Persistence context defines a scope under which particular entity instances are created, persisted, and removed.
  5. A persistence context is like a cache which contains a set of persistent entities , So once the transaction is finished, all persistent objects are detached from the EntityManager’s persistence context and are no longer managed.

Solution 2

The @PersistenceContext annotation in your code is being used to indicate that the EntityManager must be automatically injected, in other words its lifecycle will be managed by the container running your application (which is a good thing). The other option would be having all required configurations provided by you (application managed) via different options, all of them cumbersome (config files or beans) and running the risk of tying your application to some environment-specific configuration (which is a bad thing).

Solution 3

@PersistenceContext is JPA standard annotation which gives you better control of which persistence context you are Injecting.

Solution 4

My answer comes after quite a few years but here goes .

this annotation @PersistentContext works in conjunction with another bean defined in your application context:

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

The way the two work together is that the PersistenceAnnotationBeanPostProcessor will lookup the appropriate EntityManagerFactory to inject the entity manager where we have attributes annotated with @PersistenceContext

My understanding is based on the answers to this question: so question here

Share:
11,645
Admin
Author by

Admin

Updated on June 09, 2022

Comments

  • Admin
    Admin almost 2 years

    I am new in Spring Boot and trying to create a basic REST example in Spring boot. I am taking help from Spring Boot REST example website to create a basic example.

    Most of the things are clear to me but I am stuck with one annotation which is being used to fetch the data from the database with the code as below

    package com.springbootrest.repository;
    
    import java.util.List;
    
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.transaction.Transactional;
    
    import org.springframework.stereotype.Repository;
    
    import com.springbootrest.model.BookDetails;
    
    @Transactional
    @Repository
    public class BookDetailsRepoImpl implements BookDetailsRepo {
    
     @PersistenceContext
     private EntityManager entityManager;
    
     public List<BookDetails> listBookDetails() {
     return (List<BookDetails>) entityManager.createQuery("FROM BookDetails").getResultList();
     }
    
    }
    

    I don't understand how @PersistenceContext is actually working - can anyone please explain?.

  • Asma Rahim Ali Jafri
    Asma Rahim Ali Jafri over 4 years
    Can you please explain what exactly is an "entity instance" and how is it different from a persistent identity or a persistent object? I am new to all this and I couldn't find any helpful documentation online. Your answer has been the most helpful yet though :)
  • saravana kumar ramasamy
    saravana kumar ramasamy almost 3 years
    @AsmaRahimAliJafri An Entity is an table relation. instance is an object which going to for that entity. persistence Object is an entity instance which you have managed to persist via entity manager using em.diffmethods()
  • Nakamoto
    Nakamoto over 2 years
    the link is broken