簡體   English   中英

Spring JPA - 刪除所有子實體而不是一個

[英]Spring JPA - Removing all child entities instead of one

我有一個 Spring 啟動應用程序,它使用 Spring JPA 對父/子、OneToMany 數據庫關系執行操作。 一段時間以來,我一直在毫無問題地執行保存和獲取請求,但是我現在需要從子數據庫表中刪除一個子實體,但是當我測試我的代碼時,我發現它從數據庫和父實體中刪除了所有子實體這不是我要尋找的行為。

下面是實體類,Zoo 是父類,Animal 是子類。 他們應該有一個一對多的關系。

父實體。

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.fasterxml.jackson.annotation.JsonManagedReference;

@Entity
@Table(name = "ZOOS")
public class Zoo {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, unique = true)
    private Integer id;
    
    @ManyToOne
    @JoinColumn(name="name")
    private String name;
    
    @OneToMany(mappedBy = "zoo", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @Fetch(value = FetchMode.SUBSELECT)
    @JsonManagedReference
    private List<Animal> animal;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public List<Animal> getAnimal() {
        return animal;
    }

    public void setAnimal(List<Animal> animal) {
        this.animal = animal;
    }
    
}

子實體

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonBackReference;

@Entity
@Table(name = "ANIMALS")
public class Animal {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, unique = true)
    private Integer id;
    
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "zooId")
    @JsonBackReference
    private Zoo zoo;
    
    @Column(name = "species", nullable = false)
    private String species;


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Zoo getZoo() {
        return zoo;
    }

    public void setZoo(Zoo zoo) {
        this.zoo = zoo;
    }

    public String getSpecies() {
        return species;
    }

    public void setSpecies(String species) {
        this.species = species;
    }

}

動物(子)實體的回購

import org.springframework.data.jpa.repository.JpaRepository;

import uk.co.example.api.entities.Animal;

public interface AnimalRepository extends JpaRepository<Animal, Integer> {

}

調用java方法刪除動物實體

    @Autowired
    AnimalRepository animalRepo;
    
    public void deleteAnimal(Integer animalId) {
        animalRepo.deleteById(animalId);
    }
    

該方法應該從 Animal 數據庫表中刪除一只動物,但實際上它是從 Zoo 數據庫表中刪除具有相同 zooId 和動物園的所有動物。

我已經研究並嘗試將 Animal 實體 class 中 ManyToOne 注釋上的 CascadeType.ALL 更改為 PERSIST 並且我嘗試完全刪除 cascade 參數,在這兩種情況下我發現我的應用程序沒有錯誤但沒有動物記錄會被完全刪除。 這些表將與運行該方法之前位於相同的 state 中。

我還嘗試在 Zoo 實體 class 的 OneToMany 注釋上使用“orphanRemoval = true”,但這在測試時似乎沒有任何影響。

@OneToMany(mappedBy = "zoo", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    @Fetch(value = FetchMode.SUBSELECT)
    @JsonManagedReference
    private List<Animal> animal;

任何幫助將不勝感激。

Animal 和 Zoo 的關系是錯誤的

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "zooId")
@JsonBackReference
private Zoo zoo;

對於 CascateType.ALL,如果您還刪除了一個 Animal,Zoo 將被刪除,這將發出刪除所有動物的命令。

您應該刪除級聯,因為在大多數情況下它沒有意義

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM