I'm trying to delete the parent student or parent course and I get this error:
Caused by: org.postgresql.util.PSQLException: ERROR: update or delete on table "student" violates foreign key constraint "fkeyvuofq5vwdylcf78jar3mxol" on table "registration"
RegistrationId class is a composite key used in Registration class. I'm using Spring data jpa and spring boot.
What am I doing wrong? I know that putting cascadetype.all should also remove the children when the parent is deleted but it is giving me an error instead.
@Embeddable
public class RegistrationId implements Serializable {
@JsonIgnoreProperties("notifications")
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "student_pcn", referencedColumnName="pcn")
private Student student;
@JsonIgnoreProperties({"teachers", "states", "reviews"})
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "course_code", referencedColumnName="code")
private Course course;
Registration class
@Entity(name = "Registration")
@Table(name = "registration")
public class Registration {
@EmbeddedId
private RegistrationId id;
I made it work by using hibernate @OnDelete annotation. Some how the JPA.persistence CascadeTypes were not working. They had no effect for whichever I chose.
Just like below. Now I can remove the parent Student or the parent Course and all children(Registrations) are deleted with them.
@Embeddable
public class RegistrationId implements Serializable {
@JsonIgnoreProperties("notifications")
@OnDelete(action = OnDeleteAction.CASCADE)
@OneToOne
@JoinColumn(name = "student_pcn", referencedColumnName="pcn")
private Student student;
@JsonIgnoreProperties({"teachers", "states", "reviews"})
@OnDelete(action = OnDeleteAction.CASCADE)
@OneToOne
@JoinColumn(name = "course_code", referencedColumnName="code")
private Course course;