繁体   English   中英

向现有@Entity (jpa) 添加新字段时出现 SQLException

[英]SQLException while adding new fields to existing @Entity (jpa)

我有一个模型,例如看起来像这样:

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Map;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Getter
public class MyTable {

    @Id
    long myId;

    @NotNull
    String myField_1;

    @ElementCollection(fetch = FetchType.EAGER)
    @NonNull
    Map<Integer, BigDecimal> myField_2;
}

我在服务器上运行它,应用程序(一个 Spring Boot 应用程序)在这个表上工作了一段时间,我决定我需要另一个字段:

    @ElementCollection(fetch = FetchType.EAGER)
    @NonNull
    Map<Integer, BigDecimal> myAdditionalField;

但是当我再次在服务器上运行应用程序时,我得到了这样的异常:

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table my_table_my_additional_field add constraint FK35gbk1v4oaclmxu6onjiopln0 foreign key (my_table_my_id) references my_table (my_id)" via JDBC Statement
...
Caused by: java.sql.SQLException: Can't create table `my_database_name`.`#sql-7f7_4531` (errno: 150 "Foreign key constraint is incorrectly formed")

为什么会发生这种情况以及如何向我的实体添加新字段? 我有一个属性jpa.hibernate.ddl-auto: update 最有趣的部分是该字段及其“地图集合”已正确添加到数据库中,只是这个新字段存在这个奇怪的约束错误

搜索该错误消息。

这是列出一些选项的一个答案: https : //stackoverflow.com/a/56976063/1766831

暂无
暂无

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

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