Home > Mobile >  cascade all with one to one doesn't work on delete action as expected
cascade all with one to one doesn't work on delete action as expected

Time:11-10

I would like when I delete an entity the other entity get deleted

here's the first entity

@Table(name = "qualification")
public class Qualification {

        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "QUALIFICATION_PK_SEQ")
        @SequenceGenerator(name = "QUALIFICATION_PK_SEQ", sequenceName = "QUALIFICATION_PK_SEQ", allocationSize = 1)
        private Long id;

        @JsonManagedReference
        @OneToOne(cascade = CascadeType.ALL)
        private User user;
//attributes, getters, setters
}

the other entity

@Table(name = "user")
    public class User {
    
            @Id
            @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_PK_SEQ")
            @SequenceGenerator(name = "USER_PK_SEQ", sequenceName = "USER_PK_SEQ", allocationSize = 1)
            private Long id;
    
  @OneToOne(cascade = CascadeType.ALL)
    private Qualification qualification;
//attributes, getters, setters
}

so when I delete a user I got this error

oracle.jdbc.OracleDatabaseException: ora-02292 integrity constraint child record found

thanks in advance

CodePudding user response:

You have to make one side the "owner" and the other side the "inverse side".

With your mapping, you have to separate relationships between User and Qualification.

You can do this with the mappedBy attribute:

@OneToOne(cascade = CascadeType.ALL, mappedBy = "user")  
private Qualification qualification;

So mappedBy points to the owner side of the relationship.

  • Related