问题描述
我将实体 UserInfoEntity
、变量名称从moblie"更改为mobile",然后我重新启动了服务器.当我查看表格时,我发现表格没有删除移动"列.这是我的实体变化;
I changed the entity UserInfoEntity
, variable name from 'moblie' to 'mobile', and then I restarted my server. when I looked at the table i found that the table hasn't removed the column 'moblie'. Here is my change in entity;
从此;
@Entity
@Table(name = "pe_userinfo")
public class UserInfoEntity {
private String moblie;
}
到这里;
@Entity
@Table(name = "pe_userinfo")
public class UserInfoEntity {
private String mobile;
}
推荐答案
使用 auto-ddl=create-drop
来彻底删除有问题的架构 &使用固定的列值重新创建它,您不能以这种方式更新架构.
Use auto-ddl=create-drop
to drop the faulty schema in its entirety & recreate it with fixed column value, you cannot have update schema in such manner.
还要检查 Hibernate: hbm2ddl.auto=update in production?,正如它所说,最好由您自己手动处理此类情况,而不是使用 Hibernate 来处理对现有架构的此类修改.
Also check Hibernate: hbm2ddl.auto=update in production?, as it says, it is better to handle such cases manually by yourself rather than using Hibernate to handle such modifications on an existing schema.
额外的想法
如果您想保存数据;
- 您可以创建一个单独的表来保存您的数据
pe_userinfo_temp
- 部署您的产品,auto-ddl 将创建
pe_userinfo_temp
- 使用jpa逻辑从
pe_userinfo
复制数据->pe_userinfo-temp
- 从数据源手动删除表
pe_userinfo
- 在
pe_userinfo
中修复您的列,auto-ddl 将创建它但将为空 - 使用类似的jpa逻辑从
pe_userinfo_temp
复制数据 -> newpe_userinfo
- 然后最后从您的源代码中删除
pe_userinfo_temp
&从数据源中删除
- You can create a separate table to hold your data
pe_userinfo_temp
- deploy your product, auto-ddl will create
pe_userinfo_temp
- use jpa logic to copy data from
pe_userinfo
->pe_userinfo-temp
- drop the table
pe_userinfo
manually from datasource - fix your column in
pe_userinfo
, auto-ddl will create it but will be empty - use similar jpa logic to copy data from
pe_userinfo_temp
-> newpe_userinfo
- then finally drop the
pe_userinfo_temp
from your source code & drop from datasource
这篇关于如何通过使用 auto-ddl=update 更改 JPA 中的实体来更改表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!