hakima maarouf
hakima maarouf

Reputation: 2310

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

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

Upvotes: 0

Views: 35

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36203

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.

Upvotes: 1

Related Questions