Home > Net >  Spring Boot JPA. Removing entity in oneToMany relationships
Spring Boot JPA. Removing entity in oneToMany relationships

Time:03-23

This is my entity

package com.nimesia.sweetvillas.entities;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "specs", schema = "crm")
public class SpecEntity extends AbsEntity {

    @Id
    @Column(name = "spec_id")
    private @Getter @Setter String id;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinTable(
            name = "specs_translations",
            schema="crm",
            joinColumns = @JoinColumn(name = "spec_id"),
            inverseJoinColumns = @JoinColumn(name = "translation_id")
    )
    private @Getter @Setter
    List<TextEntity> texts;

}

As you can see there is a oneToMany relationships with TextEntity. Such relationship is generated through the specs_translations table.

Here comes the problem.

When it comes to creating the specEntity I can create also its subentities (translations, in this case). In the db, a reference in specs_translations and a record in translations (table which contains the records for textEntities) will be created. As it should be.

But when it comes to updating and removing a TextEntity from my SpecEntity, while the reference in specs_translations gets removed, the relative translation record stays. This leads to a situation such as the one depicted in the following pics

enter image description here enter image description here

How can I delete the record in translations when I remove its reference in specs_translations? I would like to achieve this through JPA and not through the DB.

CodePudding user response:

I solved it. I just set orphanRemoval to "true".

  • Related