英文:
Table not created in MySql DB after starting Spring Boot application
问题
项目使用了MySQL数据库和Open Server。以下是我的application.properties文件:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:8888/citatnik_bd
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true
这是我的实体类:
import javax.persistence.*;
@Entity
@Table(name="Quotes")
public class Quotes {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long ID;
private String ru, by, ua, en;
public Long GetID() { return ID; }
public void SetID(Long ID) { this.ID = ID; }
public String GetBy() { return by; }
public void SetBy(String by) { this.by = by; }
public String GetRu() { return ru; }
public void SetRu(String ru) { this.ru = ru; }
public String GetUa() { return ua; }
public void SetUa(String ua) { this.ua = ua; }
public String GetEn() { return en; }
public void SetEn(String en) { this.by = en; }
}
顺便提一下,pom.xml文件中的所有必要依赖项都正确注册:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.Citatnik</groupId>
<artifactId>Citatnik</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Citatnik</name>
<description>Citatnik project</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这是Spring应用程序的日志输出:
:: Spring Boot :: (v3.1.2)
2023-07-31T19:01:34.457+03:00 INFO 3336 --- [ restartedMain] c.Citatnik.Citatnik.CitatnikApplication : Starting CitatnikApplication using Java 17.0.6 with PID 3336 (C:\Users\dddry\OneDrive\Рабочий стол\Цитатник\Citatnik\target\classes started by dddry in C:\Users\dddry\OneDrive\Рабочий стол\Цитатник\Citatnik)
2023-07-31T19:01:34.461+03:00 INFO 3336 --- [ restartedMain] c.Citatnik.Citatnik.CitatnikApplication : No active profile set, falling back to 1 default profile: "default"
2023-07-31T19:01:34.539+03:00 INFO 3336 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-07-31T19:01:34.539+03:00 INFO 3336 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2023-07-31T19:01:35.213+03:00 INFO 3336 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-07-31T19:01:35.232+03:00 INFO 3336 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 11 ms. Found 0 JPA repository interfaces.
2023-07-31T19:01:35.866+03:00 INFO 3336 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-07-31T19:01:35.876+03:00 INFO 3336 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-07-31T19:01:35.876+03:00 INFO 3336 --- [ restartedMain] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.11]
2023-07-31T19:01:35.945+03:00 INFO 3336 ---
<details>
<summary>英文:</summary>
The project uses a database MySql, Open Server. Here is my application.properties file:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:8888/citatnik_bd
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true
Here is my entity:
import javax.persistence.*;
@Entity
@Table(name="Quotes")
public class Quotes {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long ID;
private String ru, by, ua, en;
public Long GetID() { return ID; }
public void SetID(Long ID) { this.ID = ID; }
public String GetBy() { return by; }
public void SetBy(String by) { this.by = by; }
public String GetRu() { return ru; }
public void SetRu(String ru) { this.ru = ru; }
public String GetUa() { return ua; }
public void SetUa(String ua) { this.ua = ua; }
public String GetEn() { return en; }
public void SetEn(String en) { this.by = en; }
}
By the way, everything is fine in the pom.xml file, all the necessary dependencies are registered:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.Citatnik</groupId>
<artifactId>Citatnik</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Citatnik</name>
<description>Citatnik project</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Here is logging out from Spring application:
:: Spring Boot :: (v3.1.2)
2023-07-31T19:01:34.457+03:00 INFO 3336 --- [ restartedMain] c.Citatnik.Citatnik.CitatnikApplication : Starting CitatnikApplication using Java 17.0.6 with PID 3336 (C:\Users\dddry\OneDrive\Рабочий стол\Цитатник\Citatnik\target\classes started by dddry in C:\Users\dddry\OneDrive\Рабочий стол\Цитатник\Citatnik)
2023-07-31T19:01:34.461+03:00 INFO 3336 --- [ restartedMain] c.Citatnik.Citatnik.CitatnikApplication : No active profile set, falling back to 1 default profile: "default"
2023-07-31T19:01:34.539+03:00 INFO 3336 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-07-31T19:01:34.539+03:00 INFO 3336 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2023-07-31T19:01:35.213+03:00 INFO 3336 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-07-31T19:01:35.232+03:00 INFO 3336 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 11 ms. Found 0 JPA repository interfaces.
2023-07-31T19:01:35.866+03:00 INFO 3336 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-07-31T19:01:35.876+03:00 INFO 3336 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-07-31T19:01:35.876+03:00 INFO 3336 --- [ restartedMain] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.11]
2023-07-31T19:01:35.945+03:00 INFO 3336 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-07-31T19:01:35.946+03:00 INFO 3336 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1405 ms
2023-07-31T19:01:36.094+03:00 INFO 3336 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-07-31T19:01:36.148+03:00 INFO 3336 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.2.6.Final
2023-07-31T19:01:36.149+03:00 INFO 3336 --- [ restartedMain] org.hibernate.cfg.Environment : HHH000406: Using bytecode reflection optimizer
2023-07-31T19:01:36.272+03:00 INFO 3336 --- [ restartedMain] o.h.b.i.BytecodeProviderInitiator : HHH000021: Bytecode provider name : bytebuddy
2023-07-31T19:01:36.396+03:00 INFO 3336 --- [ restartedMain] o.s.o.j.p.SpringPersistenceUnitInfo : No LoadTimeWeaver setup: ignoring JPA class transformer
2023-07-31T19:01:36.409+03:00 INFO 3336 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-07-31T19:01:36.664+03:00 INFO 3336 --- [ restartedMain] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@41c02a96
2023-07-31T19:01:36.666+03:00 INFO 3336 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2023-07-31T19:01:36.824+03:00 INFO 3336 --- [ restartedMain] o.h.b.i.BytecodeProviderInitiator : HHH000021: Bytecode provider name : bytebuddy
2023-07-31T19:01:37.044+03:00 INFO 3336 --- [ restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2023-07-31T19:01:37.048+03:00 INFO 3336 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-07-31T19:01:37.094+03:00 WARN 3336 --- [ restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2023-07-31T19:01:37.542+03:00 INFO 3336 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2023-07-31T19:01:37.574+03:00 INFO 3336 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-07-31T19:01:37.585+03:00 INFO 3336 --- [ restartedMain] c.Citatnik.Citatnik.CitatnikApplication : Started CitatnikApplication in 3.679 seconds (process running for 4.084)
I checked the port, database name, username and password - everything is correct. However, when I run the application and go to phpMyAdmin, I see a message that no tables were found in the database. What could be the issue here?
</details>
# 答案1
**得分**: 1
你正在使用spring-boot 3.1.2,这意味着你应该将你的
import javax.persistence.*;
替换为
import jakarta.persistence.*;
关于你的Maven依赖,你应该移除这个
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
因为你使用了
spring-boot-starter-data-jpa
它依赖于Hibernate。Hibernate然后依赖于jakarta.persistence API,使其类可用于你的应用程序代码。
关于你的问题,应用程序启动后你看不到表格。
只需尝试替换应用程序属性参数
spring.jpa.hibernate.ddl-auto=create
在首次运行应用程序后,将其替换为 `update`
<details>
<summary>英文:</summary>
Your are using spring-boot 3.1.2, it mean you should replace your
import javax.persistence.*;
with
import jakarta.persistence.*;
About your maven dependency you should to remove this one
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
because you use
spring-boot-starter-data-jpa
it has dependencies on Hibernate. Hibernate then has a dependency on the jakarta.persistence API which makes its classes available to your application's code.
About your problem, you don't see the tables after application start.
Just try to replace an application properties param
spring.jpa.hibernate.ddl-auto=create
And after first run of application
replace with `update`
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论