使用Hibernate删除对象的问题

我有实体项目,项目阶段和任务。

public class Project {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "project_gen")
    @Column (name = "project_id", nullable = false, length = 10)
    private Integer projectId;

    @OneToMany(mappedBy = "project", cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    @Fetch(FetchMode.SUBSELECT)
    private Collection<ProjectPhase> projectPhases;

}

public class ProjectPhase {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "phase_gen")
    @Column(name = "phase_id", nullable = false, length = 10)
    private Integer phaseId;

    @OneToMany(mappedBy = "phase", cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
    @Fetch(FetchMode.SUBSELECT)
    private Collection<Task> tasks;

    @ManyToOne
    @JoinColumn(name = "project_id", referencedColumnName = "project_id")
    private Project project;

public class Task {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tasks_gen")
    @Column(name = "task_id", nullable = false, length = 10)
    private Integer taskId;

    @ManyToOne
    @JoinColumn(name = "phase_id", referencedColumnName = "phase_id")
    private ProjectPhase phase;

我试图使用jpa .delete(projectPhase)方法删除Projectphase对象,代码正在执行,但是当我查看数据库时,“已删除”对象仍然存在。我不确定是什么问题,因为控制台没有任何错误。

我得到这样的ProjectPhase对象

  ProjectPhase phase = projectPhaseRepository.findById(phaseId).orElseThrow(() -> new Exception("Can't get phase", HttpStatus.BAD_REQUEST));

并尝试删除

projectPhaseRepository.delete(phase);
评论