What is the difference between LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean? What is the difference between LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean? spring spring

What is the difference between LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean?


Basically JPA specification defines two types of entity managers. They are :

i) Application-Managed : Application Managed entity manager means "Entity Managers are created and managed by merely the application ( i.e. our code )" .

ii) Container Managed : Container Managed entity manager means "Entity Managers are created and managed by merely the J2EE container ( i.e. our code doesn't directly manages instead entity managers are created and managed by container , and our code gets EM's through some way like using JNDI ).

Note : Created and Managed (above) means "opening , closing and involving entity manager in transactions"

LocalContainerEntityManagerFactoryBean - container managed
LocalEntityManagerFactoryBean - application managed

A Big Note : For spring based applications, the difference is not much. Spring only plays roles ( as container if you configure LocalContainerEntityManagerFactoryBean and as application if you configure LocalEntityManagerFactoryBean)


The documentation says it all:

LocalContainerEntityManagerFactoryBean -- From the link: FactoryBean that creates a JPA EntityManagerFactory according to JPA's standard container bootstrap contract.

LocalEntityManagerFactoryBean -- From the link: FactoryBean that creates a JPA EntityManagerFactory according to JPA's standard standalone bootstrap contract.

Essentially, the only difference is in how they create the JPA EntityManagerFactory.


LocalEntityManagerFactoryBean

is the simplest and the most limited. You cannot refer to an existing JDBCDataSource bean definition and no support for global transactions exists.

LocalContainerEntityManagerFactoryBean

is the most powerful JPA setupoption, allowing for flexible local configuration within the application. It supports links to an existing JDBC DataSource, supports both local and global transactions

REF: spring-framework-reference.pdf "Spring 3"