spring – What is the difference between LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean?

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 doesnt directly manages instead entity managers are created and managed by container , and our code gets EMs 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 JPAs standard container bootstrap contract.

LocalEntityManagerFactoryBean — From the link: FactoryBean that creates a JPA EntityManagerFactory according to JPAs standard standalone bootstrap contract.

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

spring – What is the difference between LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean?

LocalEntityManagerFactoryBean

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

LocalContainerEntityManagerFactoryBean

is the most powerful JPA setup
option, 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

Leave a Reply

Your email address will not be published.