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

How to solve the problem that Spring Data Jpa entity classes fail to create database tables automatically

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to solve the problem of Spring Data Jpa entity class automatic creation of database table failure", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to solve the problem of Spring Data Jpa entity class automatic creation of database table failure" bar!

Catalogue

Spring Data Jpa entity class failed to automatically create database table

After looking for it for a long time, it was found that it was a configuration problem.

Summary of problems that may cause JPA to fail to build tables automatically

1. Do not add @ Entity or mislead the package where Entity resides

2. Update is not set for ddl-auto in jpa configuration.

3. The package of the entity class is not a subpackage of the package where the startup program resides.

4. Mysql configuration problem

5. Incomplete dependence

6. Error in the relationship between entity classes

7. Annotation problem of startup class

Spring Data Jpa entity class failed to automatically create database table

First of all, let's talk about the problem I encountered. First of all, I created a spring boot project through maven and introduced Spring data jpa. As a result, after the entity class was created, I ran the project without automatically creating data tables in the database.

After looking for it for a long time, it was found that it was a configuration problem.

The configuration of the hibernate.ddl-auto node can be configured in two ways. I use the properties file to configure it:

# DataSource Configspring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:6033/data_service?characterEncoding=utf8spring.datasource.username=rootspring.datasource.password=root spring.jpa.show-sql= truespring.jpa.hibernate.ddl-auto=updatespring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialectspring.jackson.serialization.indent_output=false

The value of the hibernate.hbm2ddl.auto node has several create, create-drop, update, validate, none

Create: each time the hibernate is loaded, the table will be created automatically, and the previous table will be overwritten when starting later, so this value is basically not used, which will seriously lead to data loss.

Create-drop: each time the hibernate is loaded, the table is generated according to the model class, but as soon as sessionFactory is closed, the table is automatically deleted and recreated the next time it starts.

Update: create a database table based on the entity class model when loading hibernate. The table name is based on the value annotated by @ Entity or @ Table. The sessionFactory closed table will not be deleted, and the next startup will update the structure according to the entity model or a new entity class will create a new table.

Validate: verify the structure of the table at startup and will not create the table

None: do nothing at startup

Summary of problems that may cause JPA to fail to build tables automatically

JPA automatic table creation function is used when developing a personal project. After running the project, it is impossible to build the table automatically, and it is not wrong to report it. All kinds of methods on the Internet have been tried, but no problems have been found all night. I just found out this morning that it was one of my own sb mistakes. Simply summarize the problems that may cause JPA to fail to build tables automatically (mysql).

1. Do not add @ Entity or mislead the package where Entity resides

The target entity class needs to be annotated with @ Entity

The required Entity is located under the javax.persistence package

2. Update is not set for ddl-auto in jpa configuration.

If ddl-auto is none, the program cannot modify db. You need to set update or create.

If update is set, the program will modify the existing tables in db or create no tables. If create is set, the tables in db will be drop-create.

Be careful not to write the wrong level in yml. Jpa is located directly under spring.

3. The package of the entity class is not a subpackage of the package where the startup program resides.

When the project starts, the framework automatically scans all subpackages of the package in which the startup class resides. Therefore, the entity class package should be a child of the package where the startup class resides, such as:

Otherwise, add an EntityScan comment before the startup class to indicate the entity package to be scanned

4. Mysql configuration problem

You need to import the com.mysql.cj.jdbc.Driver driver above mysql6, and add some parameters after url.

5. Incomplete dependence

Need to introduce:

Equal dependence

6. Error in the relationship between entity classes

If the correspondence between entities is wrong, or the attribute of mapby is inconsistent with the attribute name of the corresponding entity (I am the problem), the table will not be created, and no error will be reported at the start of the project, and an error may be reported at run time.

7. Annotation problem of startup class

Thank you for your reading, the above is "how to solve the problem of Spring Data Jpa entity class automatic creation of database table failure" content, after the study of this article, I believe you on how to solve the problem of Spring Data Jpa entity class automatic creation of database table failure of this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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