繁体   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