What is PersistenceContext
A persistence context handles a set of entities which hold data to be persisted in some persistence store (e.g. a database).Entities are managed by javax.persistence.EntityManager instance using persistence context.Each EntityManager instance is associated with a persistence context. Within the persistence context, the entity instances and their lifecycle are managed.Persistence context defines a scope under which particular entity instances are created, persisted, and removed.
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
A Cache is a copy of data, copy meaning pulled from but living outside the database.Flushing a Cache is the act of putting modified data back into the database.
A PersistenceContext is essentially a Cache(copy of data from database outside database). It also tends to have it’s own non-shared database connection.An EntityManager represents a PersistenceContext (and therefore a Cache).An EntityManagerFactory creates an EntityManager(and therefore a PersistenceContext/Cache)
What is the difference between LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean?
JPA specification defines two types of entity managers.
|Entity Managers are created and managed by merely the application(“opening , closing and involving entity manager in transactions”)
||Entity Managers are created and managed by merely the J2EE container
|creates a JPA EntityManagerFactory according to JPA’s standard standalone bootstrap contract
||creates a JPA EntityManagerFactory according to JPA’s standard container bootstrap contract.
|Application-managed EntityManagers are created by an EntityManagerFactory obtained by calling the createEntityManagerFactory()
||container-managed EntityManagerFactorys are obtained through PersistenceProvider’s createContainerEntityManagerfactory()
|LocalEntityManagerFactoryBean produces an application-managed Entity- ManagerFactory.
||LocalContainerEntityManagerFactoryBean produces a container-managed EntityManagerFactory
The only real difference between application-managed and container-managed entity manager factories, as far as Spring is concerned, is how each is configured in the Spring application context.
When to use JPA getSingleResult()
getSingleResult() is used in situations like: “I am totally sure that this record exists. I don’t want to test for null every time I use this method because I am sure that it will not return it. Otherwise it causes alot of boilerplate and defensive programming. And if the record really does not exist (as opposite to what we’ve assumed), it is much better to have NoResultException compared to NullPointerException. If the record is not found it throws NoResultException
When the JPA Entities will become detached
- When the transaction (in transaction-scoped persistence context) commits, entities managed by the persistence context become detached.
- If an application-managed persistence context is closed, all managed entities become detached.
- Using clear method
- using detach method
- In extended persistence context when a stateful bean is removed, all managed entities become detached.
What is JPADialect
Dialect is “the variant of a language”.Speaking about Hibernate in context hibernate works with different databases.hibernate has to use database specific SQL. Hibernate uses “dialect” configuration to know which database you are using so that it can switch to the database specific SQL generator code wherever/whenever necessary
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.jpaDialect"/>
Now in the above statement I have specified that the jpaDialect I am going to use is of Hibernate Variant of implementation for JPA and Spring needs to take this into consideration
while creating entityManagerFactory.
The way it is referred is as follows
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="jpaDialect" ref="jpaDialect"/>
Why we need JPAVendorAdapter
EntityManagerFactory manages the lifecycle of the entities.Persistence Provider is the one which specifies the details about the database and connection details of the underlying database either by persistence-context.xml or by injecting instances of EntityManager using @PersistenceContext annotation. JPAVendorAdapter helps to define the persistence context which is likely to be used.
<persistence-unit name="MyPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
If we want to inject instances of EntityManager using @PersistenceContext annotation, we have to enable annotation bean processor in Spring configuration
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
Usually this line is optional because a default PersistenceAnnotationBeanPostProcessor will be registered by the and XML tags.
getSingleResult() return detached or Managed Object
objects returned from .getResultList() are managed.When you made changes on the managed objects, you do not worry about merging as those changes will be picked up by the EntityManager automatically.The managed objects will become detached when the EntityManager that is used to load that object is close(), clear() or detach(). Detached objects are not manged anymore and you should do merging to let the EntityManager pick up the changes.