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;
When you're using a relational DB, you are setting entities with relationships between these entities.
The error that you're getting means that:
You're trying to delete a record that its primary key is functioning as a foreign key in another table, thus you can't delete it.
In order to delete that record, first, delete the record with the foreign key, and then delete the original that you wanted to delete.
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;
Foreign keys guarantee that an entry will exist in another table. This is a way of ensuring data integrity. SQL will never allow you to delete this entry while it still deletes in the other table. Either (1) this is letting you know you would have made a grave mistake by deleting this thing which is required or (2) you would like to put in a cascading delete so that not only is this entry deleted but so is what is supposed to be referencing it in the other table. Information on cascading deletes can be found here and written fairly easily (https://www.techonthenet.com/sql_server/foreign_keys/foreign_delete.php). If neither of these two descriptions fits you, evaluate why your foreign key relationship exists in the first place because it probably should not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With