How to alter table by changing the entity in JPA with auto-ddl=update

陈杨华 :

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;

From this;

@Entity
@Table(name = "pe_userinfo")
public class UserInfoEntity {
    private String moblie;
}

to this;

@Entity
@Table(name = "pe_userinfo")
public class UserInfoEntity {
    private String mobile;
}
buræquete :

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.

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.

Extra Idea

If you wish to save your data;

  • 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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=111442&siteId=1