Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
Delete orphans when deleting parent manyToOne annotaion

I have two entities, 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'm using JpaRepository delete method. There is a constraint between the tables PATIENT and APPOINTMENT in database, I want to remove orphans, when I remove Patient. I added @OnDelete hibernate annotation but it doesn't work for me! Can you please tell me why? I want to keep that unidirectional relationship, can you please help me in this?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

If you want to keep using the association as unidirectional only, you can define the lazy-loaded inverse side in a field without exposing getters and setters for it:

@Entity
public class Patient {

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

This way orphanRemoval logic is applied from patients to their appointments and as a bonus you get the ability to navigate from patients to appointments in HQL queries.

Notice the mappedBy attribute which tells that appointments are responsible for the association management, so you continue to associate appointments with patients by setting patients in the many-to-one relation defined in the Appointment.

over 4 years ago · Santiago Trujillo Denunciar

0

There is no way that you could achieve that automatic behavior on the @ManyToOne side. Its just semantically incorrect, period.

Taking under consideration though, the fact that you only want to have an uni-directional mapping and do not specify the Set<Appointment> dependency on the Patient, then a kind of workaround to your situation would be to replace the @ManyToOne with a @OneToOne relationship. Then you would be able to use orphan-removal functionality:

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

Keep in mind though that if you follow this path, adapt you code and at some point you will be in need to introduce @OneToMany dependency on the `Patient' side then you will stumble upon problems. So i would recommend working out pros and cons first in relation to future possible alteration to the entity graph.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda