简体   繁体   English

Spring boot JPA:删除实体更改列

[英]Spring boot JPA: remove column on entity change

I am try to create table in MySQL database from java class using spring-boot-starter-data-jpa. 我尝试使用spring-boot-starter-data-jpa从java类创建MySQL数据库中的表。 It work pretty well except when I change/remove column name in java class. 除非我在java类中更改/删除列名,否则它工作得很好。 Here an example: 这是一个例子:

I have a class call "Staff" with 2 fields: id, name 我有一个班级电话“工作人员”,有2个字段:id,name

@Id 
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;

@Column(name = "name", length = 15)
private String name;

public Staff() {
}
// some setter and getter here

When I run my project, a "Staff" table generated exactly as I want with 2 columns: id, name. 当我运行我的项目时,一个“Staff”表生成我想要的2列:id,name。 The problem is if I split "name" into "firstname" and "lastname" like this: 问题是如果我将“name”拆分为“firstname”和“lastname”,如下所示:

@Id 
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;

@Column(name = "firstname", length = 15)
private String firstname;

@Column(name = "lastname", length = 15)
private String lastname;

public Staff() {
}
//some getter and setter here

The "Staff" table now contain 4 columns (id, name, firstname, lastname) instead of 3. Then I need to remove the "name" column myself. “Staff”表现在包含4列(id,name,firstname,lastname)而不是3.然后我需要自己删除“name”列。 Is there anyway to get rid of it automatically? 反正有没有自动摆脱它?

Looks like you use : 看起来你使用:

 spring.jpa.properties.hibernate.ddl-auto=update

78.1 Initialize a database using JPA 78.1使用JPA初始化数据库

spring.jpa.generate-ddl (boolean) switches the feature on and off and is vendor independent. spring.jpa.generate-ddl(boolean)打开和关闭该功能,与供应商无关。 spring.jpa.hibernate.ddl-auto (enum) is a Hibernate feature that controls the behavior in a more fine-grained way. spring.jpa.hibernate.ddl-auto(enum)是一个Hibernate功能,它以更细粒度的方式控制行为。 See below for more detail. 请参阅下文了解更多详情。

You can set spring.jpa.hibernate.ddl-auto explicitly and the standard Hibernate property values are none, validate, update, create, create-drop. 您可以显式设置spring.jpa.hibernate.ddl-auto,标准Hibernate属性值为none,validate,update,create,create-drop。

as you can see there is nothing new (different from hibernate) 你可以看到没有什么新东西(与休眠不同)

ddl-auto=update - generate changes but it doen't drop not mapped columns . ddl-auto = update - 生成更改不会删除未映射的列。 For example you can have a table with 10 columns and mapping only for 3 of them , in this case auto mode might drop them but for hibernate / jpa full mapping table-entity in not mandatory. 例如,您可以拥有一个包含10列的表,并且仅映射其中的3列,在这种情况下,自动模式可能会删除它们,但对于hibernate / jpa完整映射表实体不是必需的。 Here is jira ticket Alter and drop columns with hbm2ddl.auto=update created Nov 2011 and now status is 'not fixed'. 这里是jira ticket Alter和drop columns with hbm2ddl.auto = update 2011年11月创建,现在状态为“未修复”。

If you update db often (your domain model is changed) , you can use ddl/dml tools like liquibase , flywaydb . 如果经常更新db(您的域模型已更改),则可以使用liquibaseflywaydb等ddl / dml工具。 You describe db changes in xml file , and execute tool , all changes will be apply automatically (with auto control what already modifyed before and what should be modifed now). 您在xml文件和执行工具中描述数据库更改,所有更改都将自动应用(使用自动控制之前已修改的内容以及现在应修改的内容)。

Just recommendation : Better use ddl tools if you don't want to guess why something is droped in production. 只是建议:如果你不想猜测为什么某些东西在生产中下垂,最好使用ddl工具。 hibernate.ddl-auto maily used for development , not for production. hibernate.ddl-auto maily用于开发,不用于生产。 In production you can use none or validate - as they are safe. 在生产中,您可以使用none或验证 - 因为它们是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM