当前位置: 首页 > 知识库问答 >
问题:

Hibernate-OneToMany ORA-02291-违反-未找到父密钥

富锦
2023-03-14

我面临错误:ORA-02291:完整性约束-调用时未找到违反描述符的父密钥。JpaRepository中的saveAll()

我的数据库是Oracle 18c

我在下面有两个表

create table FINDINGS
(
    CLAIMNO VARCHAR2(50),
    TRACEID VARCHAR2(50),
    RULE VARCHAR2(10),
    NUMBEROFFINDINGS INT,
    constraint FINDINGS_PK
        primary key (CLAIMNO, TRACEID)
)
/

create table DESCRIPTOR
(
    TBL_ROW_ID int,
    CLAIMNO VARCHAR2(50) not null,
    TRACEID VARCHAR2(50) not null,
    DESCRIPTOR VARCHAR2(4000),
    constraint DESCRIPTOR_PK
        primary key (CLAIMNO, TBL_ROW_ID, TRACEID),
    constraint DESCRIPTOR__FK
        foreign key (CLAIMNO, TRACEID) references FINDINGS
)
/

我的实体代码:

@Getter
@Setter
public class FindingsDOPK implements Serializable {

    private static final long serialVersionUID = -7629886512750674550L;
    private String claimno;
    private String traceid;
}

@Getter
@Setter
public class DescriptorDOPK implements Serializable {

    private static final long serialVersionUID = 5528285828768013262L;
    private FindingsDO findingsDO;
    private int tblRowId;

}


@Entity
@Getter
@Setter
@Table(name = "DESCRIPTOR")
@IdClass(DescriptorDOPK.class)
public class DescriptorDO implements Serializable {

    private static final long serialVersionUID = 8951191961032280402L;

    @Id
    @Column(name = "TBL_ROW_ID")
    private int tblRowId;

    @Basic
    @Column(name = "DESCRIPTOR")
    private String descriptor;

    @ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.MERGE, CascadeType.PERSIST }, optional = false)
    @JoinColumns(
            value = { @JoinColumn(name = "TRACEID", referencedColumnName = "CLAIMNO", nullable = false),
                    @JoinColumn(name = "CLAIMNO", referencedColumnName = "TRACEID", nullable = false) })
    private FindingsDO findingsDO;

}



@Entity
@Setter
@Getter
@Table(name = "FINDINGS")
@IdClass(FindingsDOPK.class)
public class FindingsDO implements Serializable {

    private static final long serialVersionUID = -3271206505226648660L;
    @Id
    @Column(name = "CLAIMNO")
    private String claimno;
    @Id
    @Column(name = "TRACEID")
    private String traceid;
    @Basic
    @Column(name = "RULE")
    private String rule;

    @Basic
    @Column(name = "NUMBEROFFINDINGS")
    private int numberoffindings;

    @OneToMany(mappedBy = "findingsDO", fetch = FetchType.LAZY, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
    private Set<DescriptorDO> descriptorDOS;

}

我的java代码:

 final List<FindingsDO> findingList = new ArrayList<>();
 final var findingsDO = new FindingsDO();
 findingsDO.setClaimno("123");
 findingsDO.setTraceid("2e22a7b2-d1bb-41ff-9465-3d21da2a31");
 findingsDO.setRule("RULE_123");
 findingsDO.setNumberoffindings(1);

 final Set<DescriptorDO> descriptorSet = new HashSet<>();
 final var descriptorDO = new DescriptorDO();
 descriptorDO.setTblRowId(1);
 descriptorDO.setDescriptor("descriptor_1");

 descriptorSet.add(descriptorDO);

 for (final DescriptorDO descriptorDO : descriptorSet) {
     descriptorDO.setFindingsDO(findingsDO);
 }

 findingsDO.setDescriptorDOS(descriptorSet);

 findingList.add(findingsDO);

 findingRepository.saveAll(findingList);

Hibernate日志

Hibernate: /* load FindingsDO */
select finding0_.TRACEID          as traceid1_8_1_,
       finding0_.CLAIMNO          as claimno2_8_1_,
       finding0_.NUMBEROFFINDINGS as numberoffindings3_8_1_,
       finding0_.RULE             as rule4_8_1_,
       descrip1_.CLAIMNO          as claimno3_7_3_,
       descrip1_.TRACEID          as traceid4_7_3_,
       descrip1_.TBL_ROW_ID       as tbl_row_id1_7_3_,
       descrip1_.TBL_ROW_ID       as tbl_row_id1_7_0_,
       descrip1_.CLAIMNO          as claimno3_7_0_,
       descrip1_.TRACEID          as traceid4_7_0_,
       descrip1_.DESCRIPTOR       as descriptor2_7_0_
from FINDINGS finding0_
         left outer join DESCRIPTOR descrip1_
                         on finding0_.TRACEID = descrip1_.CLAIMNO and finding0_.CLAIMNO = descrip1_.TRACEID
where finding0_.TRACEID = ?
  and finding0_.CLAIMNO = ?

Hibernate: /* load DescriptorDO */
select descrip0_.TBL_ROW_ID       as tbl_row_id1_7_1_,
       descrip0_.CLAIMNO          as claimno3_7_1_,
       descrip0_.TRACEID          as traceid4_7_1_,
       descrip0_.DESCRIPTOR       as descriptor2_7_1_,
       finding1_.TRACEID          as traceid1_8_0_,
       finding1_.CLAIMNO          as claimno2_8_0_,
       finding1_.NUMBEROFFINDINGS as numberoffindings3_8_0_,
       finding1_.RULE             as rule4_8_0_
from DESCRIPTOR descrip0_
         inner join FINDINGS finding1_
                    on descrip0_.CLAIMNO = finding1_.TRACEID and descrip0_.TRACEID = finding1_.CLAIMNO
where descrip0_.TBL_ROW_ID = ?
  and descrip0_.CLAIMNO = ?
  and descrip0_.TRACEID = ?

Hibernate: /* insert FindingsDO */
insert into FINDINGS (NUMBEROFFINDINGS, RULE, TRACEID, CLAIMNO) values (?, ?, ?, ?)

Hibernate: /* insert DescriptorDO */
insert into DESCRIPTOR (DESCRIPTOR, TBL_ROW_ID, CLAIMNO, TRACEID) values (?, ?, ?, ?)

我怎样才能解决这个问题?

共有1个答案

黄景胜
2023-03-14

我通过将CascadeType更改为CascadeType来修复此问题。所有的

FindingsDO:

@OneToMany(mappedBy = "findingsDO", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<DescriptorDO> descriptorDOS;

说明:

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumns(
        value = { @JoinColumn(name = "TRACEID", referencedColumnName = "TRACEID", nullable = false),
                @JoinColumn(name = "CLAIMNO", referencedColumnName = "CLAIMNO", nullable = false) })
private FindingsDO findingsDO;

我不确定背后的原因是什么,但我只是读了这篇文章,然后尝试使用所有,它的工作https://www.baeldung.com/jpa-cascade-types

 类似资料:
  • 所以这里是我的数据库: 插入表格: 批次表不断出现的错误是:ORA-02290:违反了检查约束(EJAY.batches\u DATE\u CHK) 学生表出现的错误是:ORA-02291:违反完整性约束-未找到父键 我感到困惑的是,它只显示在表格中的一些行。教职员工和课程都很好,只有这两个。如果答案很明显,我道歉,我是初学者。请帮帮忙

  • 当我做插入它给我这个错误: ORA-02291:违反完整性约束(H00300581.FK_类)-未找到父密钥 对于注册表中的所有插入,都会发生此错误。我需要帮忙,请帮我修一下。 是否来自表中的外键?可能是因为我什么都试过了,但都没用。 创建表 创建序列 培训师数据 类数据 成员数据 注册资料

  • 问题内容: 嗨,我正在Oracle SQL开发人员中开发数据库,​​试图从另一个表访问foriegn键。我目前正在使用以下CREATE语句创建的ItemOrdered表 如您所见,我具有以下前键Ords和BeltID。 现在,当我尝试运行以下语句时 它给了我以下错误 违反-找不到父密钥02291。00000-“违反完整性约束(%s。%s)-找不到父密钥” 如果需要,我已经提供了Ords CREAT

  • 我试图为和创建会话,这两个表都与相同的外键关联到登录表-但是当我为创建种子时,我得到了错误,在我的另一个问题在这个链接中,我没有以最好的方式解释,但得到了一些关于使用(党-角色-关系模型)和这段代码下面他更多的想法有关已解决的问题链接 创建表和主键 创建表和主键 创建表以及主键和外键 使用表为管理员创建会话的过程: 使用表为用户创建会话的过程: 创建管理会话时,我收到一个错误 我做错了什么?

  • 当我创建表时,我将作为主键,将作为外键从同一个表中创建,作为dep表中的外键,当我想插入显示的值时,现在我感到困惑。 该表包含以下内容: 我第一次使用以下命令行: 然后我尝试不使用,如下所示:

  • 我遇到了一些SQL外键的问题。 下面是我的表和插入SQL。 SQL ORA-02291:违反完整性约束-未找到父键 我不知道为什么我的外键是错的?