gpt4 book ai didi

java - Hibernate 不自动创建表

转载 作者:行者123 更新时间:2023-12-02 04:23:36 25 4
gpt4 key购买 nike

pom.xml:

<dependencies>
...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.3.Final</version>
</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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>

实体:

@Entity
@Table(name = "person")
public class Person {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int personId;

@Column(name = "first_name")
private String firstName;
}

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/socialmedia? 
allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver.class=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.default_schema = socialmedia
spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.show-sql = true

Hibernate 不会自动创建表,我已经设置了属性和实体类,并且依赖项在运行代码后架构仍然为空。没有显示错误消息。

最佳答案

你的策略是:

spring.jpa.hibernate.ddl-auto = create-drop

这意味着 Hibernate 创建表,并在程序运行后删除表。这就是为什么您看不到任何表格的原因。

您必须使用create(仅创建表)或update(也更改表):

 spring.jpa.hibernate.ddl-auto = update

然后我在您的 pom.xml 中没有看到 JPA 启动项目,但您有不必要的 Hibernate 依赖项。您的 pom.xml 应该如下所示:

<dependencies>
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>

spring-boot-starter-data-jpa 将自动配置 Hibernate。

关于java - Hibernate 不自动创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56619660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com