简体   繁体   中英

Spring Boot JPA. Removing entity in oneToMany relationships

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

在此处输入图像描述 在此处输入图像描述

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM