我需要在同一个表中的某一列中添加外键约束。例如,对于另一个“项目”列,其中的“I”键为“I”,而“u”列的“I”键为“u”。
我更改了我的表并添加了如下约束:ALTER TABLE PROJECT ADD CONSTRAINT FK_DEPENDENCY_PROJECT_ID外键(DEPENDENCY_PROJECT_ID)引用项目(ID);
但是,我的第二个要求是,“dependency\u project\u id”列在某些情况下可以为空。但是,如果我试图在dependency_project_id列中插入null,则会出现错误“ORA-02291:违反完整性约束-未找到父密钥”。
请注意,我使用JPA将数据持久化到数据库中。但是,如果我直接在数据库的dependency\u project\u 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的新手。我有什么遗漏或解决方法吗?非常感谢您的帮助。
您应该将依赖项\u项目\u ID映射到整数而不是int。int不能有空值。如果未设置,则该值为0,因此会违反约束。
我遇到了一些SQL外键的问题。 下面是我的表和插入SQL。 SQL ORA-02291:违反完整性约束-未找到父键 我不知道为什么我的外键是错的?
我试图为和创建会话,这两个表都与相同的外键关联到登录表-但是当我为创建种子时,我得到了错误,在我的另一个问题在这个链接中,我没有以最好的方式解释,但得到了一些关于使用(党-角色-关系模型)和这段代码下面他更多的想法有关已解决的问题链接 创建表和主键 创建表和主键 创建表以及主键和外键 使用表为管理员创建会话的过程: 使用表为用户创建会话的过程: 创建管理会话时,我收到一个错误 我做错了什么?
问题内容: 嗨,我正在Oracle SQL开发人员中开发数据库,试图从另一个表访问foriegn键。我目前正在使用以下CREATE语句创建的ItemOrdered表 如您所见,我具有以下前键Ords和BeltID。 现在,当我尝试运行以下语句时 它给了我以下错误 违反-找不到父密钥02291。00000-“违反完整性约束(%s。%s)-找不到父密钥” 如果需要,我已经提供了Ords CREAT
我最近正在了解这一点,我想知道我是否可以帮助自己,这里是我的数据库: 插入值
所以这里是我的数据库: 插入表格: 批次表不断出现的错误是:ORA-02290:违反了检查约束(EJAY.batches\u DATE\u CHK) 学生表出现的错误是:ORA-02291:违反完整性约束-未找到父键 我感到困惑的是,它只显示在表格中的一些行。教职员工和课程都很好,只有这两个。如果答案很明显,我道歉,我是初学者。请帮帮忙
我试图在过程中插入一个动态值,每次运行它时,都会出现此错误。 “详细信息”:“违反ORA-02291:完整性约束(VIEW.AA_DEFAULT_APP_LIST_FK_AA_APP)-未找到父密钥 这是我的程序。 我在SQLDeveloper中尝试了这个代码,它对我来说很好... 请帮忙。