Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the difference between injecting @ PersistenceContext and @ Resource into the DAO class

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "what is the difference between injecting @ PersistenceContext and @ Resource into the DAO class", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the difference between injecting @ PersistenceContext and @ Resource into the DAO class" can help you solve your doubts.

DAO class injection @ PersistenceContext and @ Resource difference @ PersistenceContextprivate EntityManager em

What is injected is the entity manager, which requires the configuration file persistence.xml to perform the persistence operation.

Inject a bunch of data structures that hold the state of the entity class, and you can respond differently to the different states of the entity class (four, managedh or detached, etc.) (merge,persist, etc.). In fact, it is the rule that the data is extracted from the database, processed in memory, and then returned to the database.

@ Resource

Is the resource object provided by the injection container, such as SessionContext MessageDrivenContext. Or your JNDI object specified by name

It can be understood as resource-> data source-> that is, data connection, which basically tells the program where the database is.

Why do EntityManager annotations in JPA use @ PersistenceContext

When using SpringJPA, you see the following code

@ PersistenceContext private EntityManager entityManager

So I was a little curious. This @ PersistenceContext has several meanings. If it is entity injection, it will be adopted uniformly.

Is @ Autowired all right? So I looked it up on the Internet and found that almost all EntityManager uses this annotation, so I went to find the reason.

According to the above description, the summary is as follows

@ PersistenceContext is a note proprietary to jpa, while @ Autowired is a comment that comes with spring

EntityManager is not thread-safe. When multiple requests come in, spring creates multiple threads. @ PersistenceContext is used to create one EntityManager for each thread, while @ Autowired creates only one, which is shared by all threads and may report an error.

When using EntityManager, annotate with @ PersistenceContext instead of @ Autowired

After reading this, the article "what's the difference between injecting @ PersistenceContext and @ Resource into the DAO class" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report