Removing orphans when deleting parent multi-user annotation

I have two objects related as below

@Entity
@Table(name = "APPOINTMENT")
public class Appointment {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long codeAp;

    @ManyToOne(fetch = FetchType.EAGER)
,   @OnDelete(action = OnDeleteAction.CASCADE)
    @JoinColumn(name = "codeP")
    private Patient patient;

    //attributes
    //getters and setters
    //constructors



@Entity
@Table(name = "PATIENT")
public class Patient {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long codeP;

    //attributes
    //getters and setters
    //constructors

I am using the JpaRepository removal method. There is a restriction between the PATIENT and PURPOSE tables in the database, I want to remove orphans when I delete the Patient. I added the @OnDelete hibernate annotation, but for me this does not work! Could you tell me why? I want to maintain this one-way relationship, could you help me with this?

+4
source share
2 answers

, , ​​, :

@Entity
public class Patient {

    @OneToMany(mappedBy = "patient", orphanRemoval = true)
    private Collection<Appointment> appointments;
}

, orphanRemoval HQL.

mappedBy, , , , "--", Appointment.

0

@ManyToOne. .

, , , Set<Appointment> Patient, @ManyToOne @OneToOne. orphan-removal:

    @OneToOne(fetch = FetchType.EAGER, orphanRemoval=true)
    @JoinColumn(name = "codeP")
    private Patient patient;

, , - @OneToMany "", . .

0

Source: https://habr.com/ru/post/1675984/


All Articles