I have this two class in Java
@Entity
@Table(name = "user")
public class UserDb {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private BorrowerDb borrowerDb;
}
@Entity
@Table(name = "borrower")
public class BorrowerDb {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@MapsId
@OneToOne(fetch = FetchType.LAZY, optional = false, cascade = CascadeType.REMOVE)
@JoinColumn(name = "id", nullable = false)
private UserDb user;
}
When I try to call "delete from user where id = ..." I receive the next error:
java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`borrower`, CONSTRAINT `FK_7x1elq6p8384hj65g38w08jsm` FOREIGN KEY (`id`) REFERENCES `user` (`id`))
But, why? I need that the child instance "Borrower" be deleted too.
Thanks!
Start from the following basic example: