已删除的项目继续显示在 JSON

Deleted item keeps showing in JSON

我正在开发一个简单的待办事项应用程序,您可以在其中添加待办事项之间的依赖关系,这意味着如果依赖项未完成,您无法将状态更改为真(已完成)。问题是当我删除另一个项目所依赖的项目时,json 仍然显示两个项目之间的依赖关系。我将尝试用一个例子来解释;假设您有项目 1 和项目 2。项目 1 依赖于项目 2,如果项目 2 未完成,您不能将项目 1 标记为“已完成”。但是,如果您删除项目 2,则项目之间的依赖性也将消失。因此,删除项目 2 后,我可以将项目 1 的状态更改为 true,但是当我对项目 1 发出获取请求时,json 仍然显示对项目 2 的依赖性。

这是我的 TodoItem class;

package com.erdemkara.todoapp.data.entity;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import javax.persistence.*;
import java.time.LocalDate;
import java.util.Set;

@Entity
@Table(name = "todo_items")
public class TodoItem {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(nullable = false)
    private String name;

    private String description;

    @Column(nullable = false)
    private LocalDate deadline;

    @Column(nullable = false)
    private boolean status;

    @Column(name = "todo_list_id", nullable = false)
    private int todoListId;

    @OneToMany(mappedBy = "todoItem", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonManagedReference
    private Set<Dependency> dependencies;

    public TodoItem()
    {}

    public TodoItem(int id, String name, String description, LocalDate deadline,
                    boolean status, int todoListId, Set<Dependency> dependencies)
    {
        this.id = id;
        this.name = name;
        this.description = description;
        this.deadline = deadline;
        this.status = status;
        this.todoListId = todoListId;
        this.dependencies = dependencies;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public LocalDate getDeadline() {
        return deadline;
    }

    public void setDeadline(LocalDate deadline) {
        this.deadline = deadline;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    @JsonGetter("todo_list_id")
    public int getTodoListId() {
        return todoListId;
    }

    public void setTodoListId(int todoListId) {
        this.todoListId = todoListId;
    }

    public Set<Dependency> getDependencies() {
        return dependencies;
    }

    public void setDependencies(Set<Dependency> dependencies) {
        this.dependencies = dependencies;
    }
}

依赖性class;

package com.erdemkara.todoapp.data.entity;

import com.fasterxml.jackson.annotation.*;
import javax.persistence.*;

@Entity
@Table(name = "dependencies")
public class Dependency {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private int id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "todo_item_id", nullable = false)
    @JsonBackReference
    private TodoItem todoItem;

    @Column(name = "dependency_item_id", nullable = false)
    private int dependencyItemId;

    public Dependency()
    {}

    public Dependency(int id, TodoItem todoItem, int dependencyItemId)
    {
        this.id = id;
        this.todoItem = todoItem;
        this.dependencyItemId = dependencyItemId;
    }

    public int getId() {
        return id;
    }

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

    public TodoItem getTodoItem() {
        return todoItem;
    }

    public void setTodoItem(TodoItem todoItem) {
        this.todoItem = todoItem;
    }

    public int getDependencyItemId() {
        return dependencyItemId;
    }

    public void setDependencyItemId(int dependencyItemId) {
        this.dependencyItemId = dependencyItemId;
    }
}

响应项目 1 get 请求(项目 1 依赖于项目 2 和 3);

{
    "id": 1,
    "name": "Item 1",
    "description": "Study Collections",
    "deadline": "2023-01-09",
    "status": false,
    "dependencies": [
        {
            "dependencyItemId": 3
        },
        {
            "dependencyItemId": 2
        }
    ],
    "todo_list_id": 1
}

我在删除第 2 项之前和之后得到相同的响应。但是我想得到这样的响应;

{
    "id": 1,
    "name": "Item 1",
    "description": "Study Collections",
    "deadline": "2023-01-09",
    "status": false,
    "dependencies": [
        {
            "dependencyItemId": 3
        }
    ],
    "todo_list_id": 1
}

我该如何解决这个问题?

编辑:@Zychoo 我在服务层使用了 2 种不同的删除方法。一个是删除项目的所有依赖项。另一种是删除特定的依赖;

public void deleteDependencyByDependencyItemId(int todoItemId, int dependencyItemId) {
    dependencyRepository.deleteByDependencyItemId(todoItemId, dependencyItemId);
}

public void deleteAllDependenciesByTodoItemId(int todoItemId) {
    dependencyRepository.deleteAll(dependencyRepository.findAllByTodoItemId(todoItemId));
}

这是存储库层;

public interface IDependencyRepository extends CrudRepository<Dependency, Integer> {
    @Modifying
    @Transactional
    @Query(value = "delete from dependencies d where d.todo_item_id=? and d.dependency_item_id =?", nativeQuery = true)
    void deleteByDependencyItemId(int todoItemId, int dependencyItemId);
}

你可以改变

public Set<Dependency> getDependencies() {
    return dependencies;
}

 public Set<Dependency> getDependencies() {
     return dependencies.stream().filter(dependency -> "your condition for completion").collect(Collectors.toSet());
 }

来自 spring-boot 的 ObjectMapper 使用 getter 创建一个 JSON。如果您的依赖项没有出现在 getter 的 return 值中,它也不会出现在 JSON 响应中。

我重组了TodoItem服务层的delete方法:

public void deleteItemById(int id) {
    todoItemRepository.deleteById(id);
}

对此:

public void deleteItemById(int id) {
    todoItemRepository.deleteById(id);
    dependencyService.deleteAllDependenciesByTodoItemId(id);
}

它会删除项目中的所有依赖项。现在它按我的预期工作。谢谢你的回答。