In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will introduce to you the solution to the error report of entityManagerFactory in springboot. The content of the article is good. Now I would like to share it with you. Friends who feel in need can understand it. I hope it will be helpful to you. Let's read it along with the editor's ideas.
New springboot project entityManagerFactory reported an error
Solution.
1. Check to see if the annotations are introduced correctly, entity classes and jpa.
two。 Check whether there is a conflict between the references of the package
There are three ways for spring to generate EntityManagerFactory 1.LocalEntityManagerFactoryBean
It's just for use in a simple environment. It uses the JPA PersistenceProvider automatic detection mechanism (according to JPA's Java SE bootstrapping), and in most cases, you can only define persistence unit name
For example:
two。 Get EntityManagerFactory from JNDI
This option is when your application is published on the javaee5 server. You can refer to your own application server documentation on how to publish a custom JPA provider to your application server.
Example:
Persistence units is automatically detected when the javaee server starts. In fact, it is to detect the META-INF/persistence.xml file in the application package and the persistence-unit-ref in web.xml, as well as the defined environment naming. I understand it is JNDI's name.
General application scenarios are:
Use java:/ MySqlDS in META-INF/persistence.xml to get the Datesource published by the container.
Transactions is a JTA system supported by the javaee container, such as tomcat, which can be like this
If your project is going to be deployed on tomcat, to support jta, you need to put the relevant packages under the tomcat/lib package
1) jndi configuration, you can place the configuration of jndi in the tomcat/conf/Catalina/ domain name (such as localhost) / project name. xml
Under the Context node of the file, as follows:
Jndi can also be configured in server.xml,context.xml
2) jta UserTransaction configuration
The configuration under the server.xml file GlobalNamingResources node is as follows:
Then add under the context node of the project name. xml file:
All SPRING does is inject EntityManagerFactory into the application's object through dependency injection. If you want to manage transactions, use JtaTransactionManager.
3.LocalContainerEntityManagerFactoryBean
In this option, spring acts as a container. Fully in charge of JPA.
LocalContainerEntityManagerFactoryBean creates a PersistenceUnitInfo implementation based on persistence.xml.
Not all JPA provider require load-time weaving. Hibernate doesn't need it. He he. This is not necessary.
Persistence.xml configuration:
META-INF/orm.xml
How to handle multiple persistence units. Spring provides PersistenceUnitManager unified management.
Org/springframework/orm/jpa/domain/persistence-multi.xmlclasspath:/my/package/**/custom-persistence.xmlclasspath*:META-INF/persistence.xml
Key in dataSources is the datasource name configured in persistence.xml, and value-ref is the data source managed by spring.
In addition:
EntityManagerFactory is thread-safe, but EntityManager is not.
Public class ProductDaoImpl implements ProductDao {private EntityManagerFactory emf;@PersistenceUnitpublic void setEntityManagerFactory (EntityManagerFactory emf) {this.emf = emf;} public Collection loadProductsByCategory (String category) {EntityManager em = this.emf.createEntityManager (); try {Query query = em.createQuery ("from Product as p where p.category =? 1"); query.setParameter (1, category); return query.getResultList ();} finally {if (em! = null) {em.close ();}
The biggest problem with using it this way is to create a new entityManager every time. So what are we going to do?
You can get a transactional EntityManager ("shared EntityManager") through @ PersistenceContext. Why do you call it transactional? Because it is a shared and thread-safe proxy for the current transactional EntityManager.
Public class ProductDaoImpl implements ProductDao {@ PersistenceContextprivate EntityManager em;public Collection loadProductsByCategory (String category) {Query query = em.createQuery ("from Product as p where p.category =: category"); query.setParameter ("category", category); return query.getResultList ();}} springboot is a new programming specification for springboot, which is designed to simplify the initial construction and development process of new Spring applications. SpringBoot is also a framework that serves the framework, and the scope of services is to simplify configuration files.
These are all the solutions to entityManagerFactory errors in springboot. For more information related to the solutions to entityManagerFactory errors in springboot, you can search the previous articles or browse the following articles to learn! I believe the editor will add more knowledge to you. I hope you can support it!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.