我正在開發一個簡單的 todo 應用程式,您可以在其中添加 todo 項之間的依賴項,這意味著如果依賴項未完成,您無法將狀態更改為 true(completed)。問題是當我洗掉另一個專案所依賴的專案時,json 仍然顯示兩個專案之間的依賴關系。我將嘗試用一個例子來解釋;假設您有第 1 項和第 2 項。第 1 項依賴于第 2 項,如果第 2 項未完成,您不能將第 1 項標記為“已完成”。但是,如果您洗掉第 2 項,則專案之間的依賴關系也將消失。因此,在洗掉第 2 項后,我可以將第 1 項的狀態更改為 true,但是當我對第 1 項發出獲取請求時,json 仍然顯示對第 2 項的依賴。
這是我的 TodoItem 類;
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;
}
}
依賴類;
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 項獲取請求的回應(第 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);
}
uj5u.com熱心網友回復:
你可以改變
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 的回傳值中,它就不會出現在 JSON 回應中。
uj5u.com熱心網友回復:
我從這里重新組織了 TodoItem 服務層中的洗掉方法:
public void deleteItemById(int id) {
todoItemRepository.deleteById(id);
}
對此:
public void deleteItemById(int id) {
todoItemRepository.deleteById(id);
dependencyService.deleteAllDependenciesByTodoItemId(id);
}
它會洗掉每個依賴項以及該專案。現在它按我的預期作業。謝謝你的回答。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/459060.html
