Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean?

Can anybody explain what is the difference between the Spring Framework's LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean?

like image 567
BlueSky Avatar asked May 27 '11 19:05

BlueSky


People also ask

What is LocalContainerEntityManagerFactoryBean?

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"

Why do we use LocalContainerEntityManagerFactoryBean?

The LocalContainerEntityManagerFactoryBean gives full control over EntityManagerFactory configuration and is appropriate for environments where fine-grained customization is required. The LocalContainerEntityManagerFactoryBean will create a PersistenceUnitInfo based on the persistence.

How do I use EntityManagerFactory in spring boot?

The complete example of getting EntityManager using the custom configuration in Spring Boot. Open eclipse and create maven project, Don't forget to check 'Create a simple project (skip)'click on next. Fill all details(GroupId – entitymanager, ArtifactId – entitymanager and name – entitymanager) and click on finish.


2 Answers

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)

like image 57
Prashanth Avatar answered Sep 28 '22 06:09

Prashanth


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.

like image 44
nicholas.hauschild Avatar answered Sep 28 '22 05:09

nicholas.hauschild