gpt4 book ai didi

java - ORA-02291 : integrity constraint violated - parent key not found When trying to insert null in foreign key

转载 作者:行者123 更新时间:2023-12-01 18:18:07 24 4
gpt4 key购买 nike

我有一个要求,我必须向同一个表中的特定列添加外键约束。例如,我有一个表 PROJECT,它有一个主键列“id”和另一列“dependency_project_id”,它是引用“id”列的外键。

我更改了表格并添加了如下约束:更改表项目添加约束 FK_DEPENDENCY_PROJECT_ID 外键 (DEPENDENCY_PROJECT_ID) 引用项目 (ID);

但是,我的第二个要求是,“dependency_project_id”列在某些情况下可以为空。但是,如果我尝试在 dependency_project_id 列中插入 null,则会收到错误“ORA-02291:违反完整性约束 - 未找到父键”。

请注意,我正在使用 JPA 将数据保存到数据库中。但是,如果我直接在数据库中的 dependency_project_id 列中插入 null,则可以毫无问题地执行此操作。

这是我的实体。

package com.oracle.estimationtool.model;
import java.util.List;
import javax.json.bind.annotation.JsonbTransient;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

@Entity(name = "Project")
@Table(name = "PROJECT")
@Access(value = AccessType.FIELD)
@SequenceGenerator(name = "ProjectSeq", sequenceName = "PROJECT_SEQ", allocationSize = 50)
@NamedQuery(name = "Project.findProject", query = "SELECT p from Project p where p.id = :id")
public class Project extends BaseEntity {
@Id
@GeneratedValue(generator = "ProjectSeq", strategy = GenerationType.SEQUENCE)
private Integer id;
@OneToMany
@JoinColumn(name = "PROJECT_ID")
private List<SolutionComponentEstimate> solutionComponentEstimates;
@Column(columnDefinition = "CLOB NULL", name = "BUSINESS_EVENTS", nullable = true)
@Lob
@Basic(optional = true)
private List<BusinessEvent>businessEvents;
@Column(nullable = true, precision = 10, scale = 4)
@Basic(optional = true)
private Double duration;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "PROJECT_ID")
private List<SCVersionProject> scvproject;
@Column(name = "NAME", length = 255, nullable = false)
@Basic(optional = false)
@Size(max = 255)
@NotBlank
private String name;
@Column(name = "DEPENDENCY_PROJECT_ID", nullable = true)
@Basic(optional = true)
private int dependencyProjectId;
@Column(name = "DEPENDENCY_START", nullable = true)
@Basic(optional = true)
private boolean dependencyStart;
@Column(name = "DEPENDENCY_DELTA", nullable = true)
@Basic(optional = true)
private int dependencyDelta;
@ManyToOne
@JoinColumn(name = "ROADMAP_ID")
@JsonbTransient
private Roadmap roadmap;

public Project() {
}

public Project(List<SolutionComponentEstimate> solutionComponentEstimates, List<BusinessEvent> businessEvents,
Double duration, String name) {
this.solutionComponentEstimates = solutionComponentEstimates;
this.businessEvents = businessEvents;
this.duration = duration;
this.name = name;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public List<SolutionComponentEstimate> getSolutionComponentEstimates() {
return solutionComponentEstimates;
}

public void setSolutionComponentEstimates(List<SolutionComponentEstimate> solutionComponentEstimates) {
this.solutionComponentEstimates = solutionComponentEstimates;
}

public List<BusinessEvent> getBusinessEvents() {
return businessEvents;
}

public void setBusinessEvents(List<BusinessEvent> businessEvents) {
this.businessEvents = businessEvents;
}

public Double getDuration() {
return duration;
}

public void setDuration(Double duration) {
this.duration = duration;
}

public List<SCVersionProject> getScvproject() {
return scvproject;
}

public void setScvproject(List<SCVersionProject> scvproject) {
this.scvproject = scvproject;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getDependencyProjectId() {
return dependencyProjectId;
}

public void setDependencyProjectId(int dependencyProjectId) {
this.dependencyProjectId = dependencyProjectId;
}

public boolean isDependencyStart() {
return dependencyStart;
}

public void setDependencyStart(boolean dependencyStart) {
this.dependencyStart = dependencyStart;
}

public int getDependencyDelta() {
return dependencyDelta;
}

public void setDependencyDelta(int dependencyDelta) {
this.dependencyDelta = dependencyDelta;
}

public Roadmap getRoadmap() {
return roadmap;
}

public void setRoadmap(Roadmap roadmap) {
this.roadmap = roadmap;
}

}

我是 JPA 新手。我是否缺少任何内容或有任何解决方法?非常感谢任何帮助。

最佳答案

您应该将 DEPENDENCY_PROJECT_ID 映射到 Integer 而不是 int。int 不能有空值。如果没有设置,则值为 0,因此违反约束。

关于java - ORA-02291 : integrity constraint violated - parent key not found When trying to insert null in foreign key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60327285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com