gpt4 book ai didi

java - 将实体的元素添加到同一实体 - @OneToMany

转载 作者:行者123 更新时间:2023-12-01 19:50:04 29 4
gpt4 key购买 nike

我的 Task 实体表示为:

@Entity
@Getter
@NoArgsConstructor
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NotBlank
private String name;
@Enumerated(EnumType.STRING)
private Category category;
@OneToOne
private Employee assignee;
@OneToMany
private List<Task> precedingTasks;
private String details;
private int estimatedTimeInMinutes;
private LocalDateTime deadline;
private LocalDateTime creationTime;
private LocalDateTime startTime;
private LocalDateTime endTime;

public Task(String name, Category category, Employee assignee, List<Task> precedingTasks, String details,
int estimatedTimeInMinutes, LocalDateTime deadline) {
this.name = name;
this.category = category;
this.assignee = assignee;
this.precedingTasks = precedingTasks;
this.details = details;
this.estimatedTimeInMinutes = estimatedTimeInMinutes;
this.deadline = deadline;
this.creationTime = LocalDateTime.now();
this.startTime = null;
this.endTime = null;
}

我注意到没有 precedingTasks 的任务可以正确添加,但没有。

例如:添加三个没有 precedingTasks 的任务后,我在 Postman 中发送 POST,如下所示:

{
"name": "Wyslac przesyłke nr 87",
"category": "TODO",
"assigneeId": 1,
"precedingTasksId": [1, 2, 3],
"details": "Dodac zakladke",
"estimatedTimeInMinutes": 78,
"deadline": "2013-12-18T14:30:40.100000000"
}

但是我得到了一个没有之前的任务的任务:

{
"id": 4,
"name": "Wyslac przesyłke nr 87",
"category": "TODO",
"assignee": {
"id": 1,
"firstName": "Wojciech",
"lastName": "Wasilewski",
"email": "wojtek@wasilewski.pl",
"role": "ADMIN",
"password": "h*n$GvFE",
"manager": true,
"password_LENGTH": 8,
"passwordValid": false
},
"precedingTasks": [],
"details": "Dodac zakladke",
"estimatedTimeInMinutes": 78,
"deadline": "2013-12-18T14:30:40.1",
"creationTime": "2018-08-01T13:38:45.924",
"startTime": null,
"endTime": null
}

我使用这个 Controller :

@PostMapping("/tasks")
@ResponseStatus(HttpStatus.CREATED)
public Task addNewTask(@RequestBody TaskRequest taskRequest) {
String name = taskRequest.getName();
Category category = taskRequest.getCategory();
checkIfAssigneeExists(taskRequest.getAssigneeId());
Employee assignee = employeeRepository.findById(taskRequest.getAssigneeId()).get();
List<Task> precedingTasks = new ArrayList<>();

if (taskRequest.getPrecedingTaskIds() != null)
taskRequest.getPrecedingTaskIds().forEach(id -> precedingTasks.add(taskRepository.findById(id).get()));

String details = taskRequest.getDetails();
int estimatedTimeInMinutes = taskRequest.getEstimatedTimeInMinutes();
LocalDateTime deadline = taskRequest.getDeadline();
Task task = new Task(name, category, assignee, precedingTasks, details, estimatedTimeInMinutes, deadline);
taskRepository.save(task);
return task;
}

还有这样的请求:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class TaskRequest {

@NonNull
private String name;

@NonNull
private Category category;

@NonNull
private Long assigneeId;

@NonNull
private List<Long> precedingTaskIds;

private String details;
private int estimatedTimeInMinutes;

@NonNull
private LocalDateTime deadline;
}

出了什么问题? Controller ,还是 JSON 中 ArrayList 的格式?在添加整个任务之前,我应该将 precedingTasks 添加到数据库吗?

最佳答案

TaskRequest中的字段precedingTaskIds在名称上与POST请求precedingTasksId中的字段不对应。重命名或在 Postman 中重写 POST。

关于java - 将实体的元素添加到同一实体 - @OneToMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51632887/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com