java - application.properties 中 spring.jpa.hibernate.ddl-auto=update 的问题

标签 java spring database spring-data-jpa spring-data

我的文件application.properties有问题,在这一行:

spring.jpa.hibernate.ddl-auto=update

当我从实体中删除某些属性时,这些属性仍保留在数据库中。

这是 application.properties 的副本:

spring.jpa.generate-ddl=true
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = 
org.hibernate.dialect.MySQL5Dialect
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/taskmanagement
spring.datasource.username = root
spring.datasource.password = 
server.port=89
spring.data.rest.default-media-type= application/json

如何解决这个问题?

最佳答案

如果您想基于对象映射重新创建数据库架构,只需使用:

spring.jpa.hibernate.ddl-auto=create

但是请记住,这会破坏您保存在数据库中的数据。

其他选项是使用create-drop - 这将在应用程序终止时破坏您的数据库架构。

编辑:

您无法在不破坏数据的情况下更新数据库架构。想象一下您正在更改某个字段的数据类型并且已经有了数据。数据库不知道如何转换现有数据。

关于java - application.properties 中 spring.jpa.hibernate.ddl-auto=update 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57688037/

相关文章:

java - 构建具有 Spring 框架依赖项的发行版

java - 未调用自定义注释

mysql - 为独立的 Spring Boot 应用程序设置 MySQL 数据库以在另一个设备中使用

java - 使用 Jersey 2 客户端发布空 body

Java API "Run on EDT if not on EDT"

java - Calendar#add(Calendar.MONTH, months) 和 LocalDate#plusMonth(months) 之间有什么不同的行为吗?

sql - 分解 SQL 代码 - MS SQL Server

java - 如何在 Thymeleaf 中使用 ArrayList 填充组合框?

mysql - 在一张表中建模数据与使用两张表相比的优势

mysql - 将 Laravel 应用程序连接到另一个数据库 - Laravel 5.8