无法使用 Spring Boot 和 JPA 在数据库中创建表
当我使用 Spring Boot 应用程序和 JPA 在名为 schooldb 的数据库中创建一个表时,代码运行良好,但我无法自动创建表,并且必须使用 CMD 或任何其他终端显式创建它。 IDE。
这是我的存储库类
package com.jpabasics.jpaBasics.enitity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Student {
@Id
private long studentId;
private String firstName;
private String lastName;
private String emailId;
private String guardianName;
private String guardianEmail;
private String guardianMobile;
}
,这是我的 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>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jpabasics</groupId>
<artifactId>jpaBasics</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jpaBasics</name>
<description>Jpa basics</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
另外,这是我的 application.properties 文件
# Database Config : mysql
spring.datasource.url = jdbc:mysql://localhost:3306/schooldb
spring.datasource.username = root
spring.datasource.password = Lko@6388895330
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
# Hibernate Configuration
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl.auto = update
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
有人可以帮我解决这个问题吗?
When I am using my Spring Boot application along with JPA to create a table in my database named schooldb, the code runs fine but I am unable to automatically create a table and have to explicitly create it either using the CMD or any other terminal in an IDE.
Here is my repository class
package com.jpabasics.jpaBasics.enitity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Student {
@Id
private long studentId;
private String firstName;
private String lastName;
private String emailId;
private String guardianName;
private String guardianEmail;
private String guardianMobile;
}
and here is my POM.xml file
<?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>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jpabasics</groupId>
<artifactId>jpaBasics</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jpaBasics</name>
<description>Jpa basics</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Also here is my application.properties file
# Database Config : mysql
spring.datasource.url = jdbc:mysql://localhost:3306/schooldb
spring.datasource.username = root
spring.datasource.password = Lko@6388895330
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
# Hibernate Configuration
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl.auto = update
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
Could someone help me out with this please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您缺少在数据库中构建表来托管实体所需的 @table 注释。另外,对列进行注释也是一个很好的习惯,以确保它们的命名正确(尽管在您的情况下,它们会被正确解析)。例如:
You're missing the @table annotation which is required to build a table in your DB to host the entities. Also, it's good custom to annotate your columns as well, to make sure they're named correctly (although in your case, they would've been correctly parsed). For example:
我使用相同的代码,但仍然无法创建表。
请查找以下日志
。 ____ _ __ _ _
/\ / ' __ _ () __ __ _ \ \ \
( ( )__ | '_ | '| | ' / ` | \ \ \
\/ __)| |)| | | | | || (| | ) ) ) )
' || .__|| ||| |_, | ////
=========||==============|/=///< /em>/
:: Spring Boot :: (v3.1.1)
I am using same code but still not able to create a table.
Please find below logs
. ____ _ __ _ _
/\ / ' __ _ () __ __ _ \ \ \
( ( )__ | '_ | '| | ' / ` | \ \ \
\/ __)| |)| | | | | || (| | ) ) ) )
' || .__|| ||| |_, | / / / /
=========||==============|/=////
:: Spring Boot :: (v3.1.1)