I searched the internet, tried many methods but I still have a problem during the removal.Please give me any directions because I want to understand what I'm doing wrong.
I have several entities but in brief:
public class PetOwner extends User {
private String address;
private String telefon;
@OneToMany(cascade={CascadeType.REMOVE})
private List<Appointment> appointments;
}
public class Appointment {
@Id
@GeneratedValue
private long id;
@ManyToOne(cascade={CascadeType.PERSIST})
@JoinColumn(name = "owner_id")
private PetOwner petOwner;
}
When i try delete the Appointment with this method(JpaRepository)
@Transactional
public void delete(Appointment appointment){
appointmentRepository.delete(appointment);
}
Spring throws a exception:
MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails
Only way is set in Appointment.class
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name = "owner_id")
private PetOwner petOwner;
But then in addition to the meeting also removes PetOwner. Please for directions on how to handle.