【问题标题】:Hibernate-Spring boot : spring.jpa.hibernate.ddl-auto=update recreates tables and columns after update the entityHibernate-Spring boot : spring.jpa.hibernate.ddl-auto=update 更新实体后重新创建表和列
【发布时间】:2018-08-17 20:02:23
【问题描述】:

我启动了连接到 MySQL 数据库的 Spring Boot 项目。在我的application.properties 中,我输入了spring.jpa.hibernate.ddl-auto = update,但在我更新实体中的表名后,仍会创建一个新表,并且在我更新属性的列名后会创建一个新列。

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/d-gdd?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
spring.datasource.username = root
spring.datasource.password =


## Hibernate Properties

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update

【问题讨论】:

  • 您的期望是什么? hibernate 不会删除你现有的表
  • 我不想让 hibernate 删除现有的表,而只是将它们更新为新名称,就像我们正在执行 UPDATE TABLE X RENAME TO Y

标签: java spring hibernate spring-boot


【解决方案1】:
spring.jpa.hibernate.ddl-auto = update

请理解 update 操作将尝试添加新列、约束等,但不会永远不会删除以前可能存在的列或约束,但不再像对象模型的一部分。

【讨论】:

  • 是的,但是如果我们想更改表的名称,我们该怎么办呢?
【解决方案2】:

然而,就像上面写的评论一样,我也有同样的疑惑, spring.jpa.hibernate.ddl-auto=update 只检测更改并在 db 上更新这些更改,就好像它是一个新事物一样。

所以如果你在本地运行,我建议这样做

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

但这会删除所有记录,所以如果你真的想“更新”,那就手动更新那些列名和表名。

【讨论】:

    猜你喜欢
    • 2021-04-09
    • 2017-12-25
    • 1970-01-01
    • 2015-12-15
    • 2022-08-11
    • 2020-08-02
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    相关资源
    最近更新 更多