0
votes

When I try to save with cascade all an Entity with a collection inside set as "OneToMany" it returns the error.

Error :

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

@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;
}

Here is the log with some hibernate information

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
1
Which entity the table MACHINE is mapped to? Seems it doesn't have cascade on Officer property causing FK constraint violation during flush - Alex Salauyou
there is only 2 hibernate operations being executed in this case, the MACHINE name refers to my database name... I only have those 2 tables called GEN_INFO and OFFICER - Matheus Ciaramella
(Obviously,) This is a faq. Before considering posting please always google your error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names, & read many answers. If you post a question, use one phrasing as title. See How to Ask & the voting arrow mouseover texts. - philipxy
Considering that the binding of the parameters appears to be correct, is it possible that there's something wrong with the definition of your constraint? Could you post the relevant DDLs of those tables, including constraints? - g00glen00b

1 Answers

0
votes

It seems you hold the parent key info for contact_name column of gen_info table within a table called officer and column called officer_id(assummingly), and trying to insert a value into gen_info which has no matching value for officer.officer_id