Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Autowired vs @PersistenceContext for EntityManager bean

What is the difference between:

@Autowired private EntityManager em; 

versus:

@PersistenceContext private EntityManager em; 

Both options work in my application, but can I break something by using the @Autowired annotation?

like image 705
Cosmin Vasii Avatar asked Jul 10 '15 07:07

Cosmin Vasii


People also ask

What is @PersistenceContext annotation used for?

You can use the @PersistenceContext annotation to inject an EntityManager in an EJB 3.0 client (such as a stateful or stateless session bean, message-driven bean, or servlet). You can use @PersistenceContext attribute unitName to specify a persistence unit by name, as Example 29-13 shows.

What is the difference between @bean and @autowired?

@Bean is just for the metadata definition to create the bean(equivalent to tag). @Autowired is to inject the dependancy into a bean(equivalent to ref XML tag/attribute).

How do you inject EntityManager 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.

What is difference between EntityManagerFactory and SessionFactory?

Using EntityManagerFactory approach allows us to use callback method annotations like @PrePersist, @PostPersist,@PreUpdate with no extra configuration. Using similar callbacks while using SessionFactory will require extra efforts.


2 Answers

@PersistenceContext allows you to specify which persistence unit you want to use. Your project might have multiple data sources connected to different DBs and @PersistenceContext allows you to say which one you want to operate on

check the explanation here: http://www.coderanch.com/t/481448/java-EJB-SCBCD/certification/unitName-PersistenceContext

like image 165
sashok_bg Avatar answered Oct 16 '22 13:10

sashok_bg


You shouldn't use @Autowired. @PersistenceContext takes care to create a unique EntityManager for every thread. In a production application you can have multiple clients calling your application in the same time. For each call, the application creates a thread. Each thread should use its own EntityManager. Imagine what would happen if they share the same EntityManager: different users would access the same entities.

usually the EntityManager or Session are bound to the thread (implemented as a ThreadLocal variable). 

Source: https://stackoverflow.com/a/42074452/2623162

EntityManager instances are not thread-safe.  

Source: https://docs.oracle.com/cd/E19798-01/821-1841/bnbqy/index.html

Please notice that @PersistenceContext annotation comes from javax.persistence package, not from spring framework. In JavaEE it is used by the JavaEE container (aka the application server) to inject the EntityManager. Spring borrowed the PersistenceContext annotation to do the same: to inject an application-managed (= not container-managed) EntityManager bean per thread, exactly as the JavaEE container does.

like image 40
Alex Avatar answered Oct 16 '22 11:10

Alex