如何通过使用 auto-ddl=update 更改 JPA 中的实体来更改表 [英] How to alter table by changing the entity in JPA with auto-ddl=update

查看:19
本文介绍了如何通过使用 auto-ddl=update 更改 JPA 中的实体来更改表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将实体 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复制数据 -> new pe_userinfo
  • 然后最后从您的源代码中删除 pe_userinfo_temp &从数据源中删除<​​/li>
  • 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 -> new pe_userinfo
  • then finally drop the pe_userinfo_temp from your source code & drop from datasource

这篇关于如何通过使用 auto-ddl=update 更改 JPA 中的实体来更改表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆