我有两个实体class.one是PhaseEntity,另一个是TaskEntity。 PhaseId将是TaskEntity类的外键。我可以创建值并将其保存到数据库但无法更新数据库。 TaskEntity类的一部分:
@ManyToOne(optional=false)
@JoinColumn(name="phaseId")
private PhaseEntity phaseEntity;
控制器类:
public class TaskController {
@Autowired
private TaskService taskService;
@Autowired
private PhaseService phaseService;
@RequestMapping(value="/task/create",method=RequestMethod.GET)
public String createForm(Model model,Principal principal){
model.addAttribute(new TaskEntity());
model.addAttribute("body", "task/task-create");
model.addAttribute("generaltaskDto",new GeneralTaskDto());
model.addAttribute("phaseEntities", phaseService.phaseList());
return "layouts/default";
}
@RequestMapping(value="/task/create",method=RequestMethod.POST)
public String createFormPost(Model model,GeneralTaskDto generaltaskDto,BindingResult result){
TaskEntity taskAndPhase=generaltaskDto.getTaskEntity();
taskAndPhase.setPhaseEntity(phaseService.getPhaseByPhaseId(generaltaskDto.getPhaseId()));
taskService.saveTask(taskAndPhase);
return "redirect:/task/list";
}
@GetMapping(value="/task/update/{id}")
public String updateTask(Model model,@PathVariable String id){
TaskEntity taskEntity= taskService.getTaskId(Integer.parseInt(id));
model.addAttribute("body", "task/task-create");
model.addAttribute("phaseEntities", phaseService.phaseList());
return "layouts/default";
}
GeneraltaskDto class:
public class GeneralTaskDto {
private TaskEntity taskEntity=new TaskEntity();
private Integer phaseId;
public TaskEntity getTaskEntity() {
return taskEntity;
}
public void setTaskEntity(TaskEntity taskEntity) {
this.taskEntity = taskEntity;
}
public Integer getPhaseId() {
return phaseId;
}
public void setPhaseId(Integer phaseId) {
this.phaseId = phaseId;
}
}
here is the client page of the application
任何人都可以帮助了解如何使用控制器请求更新数据库。提前谢谢。
答案 0 :(得分:0)
请尝试在updateTask
行之后的TaskEntity taskEntity= taskService.getTaskId(Integer.parseInt(id));
方法下在控制器中添加这些行。
PhaseEntity phaseEntity=taskEntity.getPhaseEntity();
generaltaskDto.setTaskEntity(taskEntity);
generaltaskDto.setPhaseId(phaseEntity.getPhaseId());
model.addAttribute("generaltaskDto", generaltaskDto);