当我尝试通过级联保存一个内部集合为“ OneToMany”的所有实体时,它将返回错误。

错误:


o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:2291,SQLState:23000
o.h.engine.jdbc.spi.SqlExceptionHelper:ORA-02291:违反完整性约束(MACHINE.OFFICER_FK)-找不到父键


@Entity(name = "GenInfo")
@Table(name = "GEN_INFO")
public class GenInfo {

    @Id
    @GeneratedValue(generator = "GEN_INFO_SEQ")
    @Column(name = "ID_GEN_INFO")
    Long id;

    @Column(name = "STREET_ADDRESSS")
    String streetAddress;

    @Column(name = "CITY")
    String city;

    @Column(name = "STATE_REGION")
    String stateOrRegion;

    @Column(name = "POSTAL_CODE")
    String PostalCode;

    @Column(name = "COUNTRY")
    String Country;

    @Column(name = "CONTACT_NAME")
    String ContactName;

    @Column(name = "CONTACT_TITLE")
    String ContactTitle;

    @OneToMany(mappedBy = "genInfo", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    List<Officer> officerCollection;

}

@Entity(name = "Officer")
@Table(name = "OFFICER")
public class Officer {

    @Id
    @GeneratedValue(generator = "OFFICER_SEQ")
    @Column(name = "ID_OFFICER")
    Long id;

    @Column(name = "OFFICER_NUM")
    String officerID;

    @Column(name = "PERSON_ID")
    String personID;

    @Column(name = "OF_RANK")
    String rank;

    @Column(name = "STATUS")
    String status;

    @Column(name = "TITLE")
    String LongTitle;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_GEN_INFO")
    private GenInfo genInfo;
}


这是带有一些休眠信息的日志

Hibernate: insert into gen_info (contact_name, contact_title, country, postal_code, city, state_region, street_addresss, id_gen_info) values (?, ?, ?, ?, ?, ?, ?, ?)
o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - []
o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - []
o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [VARCHAR] - [France]
o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [VARCHAR] - [92085]
o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [VARCHAR] - [PARIS]
o.h.type.descriptor.sql.BasicBinder      : binding parameter [6] as [VARCHAR] - []
o.h.type.descriptor.sql.BasicBinder      : binding parameter [7] as [VARCHAR] - [Tour W, 102 terrasse Boieldieu]
o.h.type.descriptor.sql.BasicBinder      : binding parameter [8] as [BIGINT] - [11]
Hibernate: insert into officer (title, id_gen_info, officer_num, person_id, of_rank, status, id_officer) values (?, ?, ?, ?, ?, ?, ?)
o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [Chairman of the Board]
o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BIGINT] - [11]
o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [VARCHAR] - [1597265]
o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [VARCHAR] - [250881]
o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [VARCHAR] - [1]
o.h.type.descriptor.sql.BasicBinder      : binding parameter [6] as [VARCHAR] - [Both]
o.h.type.descriptor.sql.BasicBinder      : binding parameter [7] as [BIGINT] - [135]
o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 2291, SQLState: 23000
o.h.engine.jdbc.spi.SqlExceptionHelper   : ORA-02291: integrity constraint (MACHINE.OFFICER_FK) violated - parent key not found

最佳答案

看来您持有的父键信息
假定在名为contact_name的表中的gen_info表的officer列和名为officer_id的列中,并尝试向gen_info中插入一个与officer.officer_id没有匹配值的值

10-07 18:10