Can anybody explain what is the difference between the Spring Framework\'s LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean?
The JPA specification defines two kinds of entity managers:
Application-managed—Entity managers are created when an application directly requests one from an entity manager factory. With application-managed entity managers, the application is responsible for opening or closing entity managers and involving the entity manager in transactions. This type of entity manager is most appropriate for use in standalone applications that don’t run in a Java EE container.
Container-managed—Entity managers are created and managed by a Java EE container. The application doesn’t interact with the entity manager factory at all. Instead, entity managers are obtained directly through injection or from JNDI. The container is responsible for configuring the entity manager factories. This type of entity manager is most appropriate for use by a Java EE container that wants to maintain some control over JPA configuration beyond what’s specified in persistence.xml.
Application-managed EntityManagers
are created by an EntityManagerFactory
obtained by calling the createEntityManagerFactory()
method of the PersistenceProvider. Meanwhile, container-managed EntityManagerFactorys are obtained through PersistenceProvider’s createContainerEntityManagerfactory()
method.
Each flavor of entity manager factory is produced by a corresponding Spring factory bean:
LocalEntityManagerFactoryBean
produces an application-managed Entity-
ManagerFactory.
LocalContainerEntityManagerFactoryBean
produces a container-managed
EntityManagerFactory
It’s important to point out that the choice made between an application-managed
EntityManagerFactory
and a container-managed EntityManagerFactory
is completely transparent to a Spring-based application. When you’re working with Spring and JPA, the intricate details of dealing with either form of EntityManagerFactory
are hidden, leaving your data-access code to focus on its true purpose: data access.
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.