2017-01-16 64 views
1

我有兩個實體class.one是PhaseEntity,另一個是TaskEntity。 PhaseId將是TaskEntity類的外鍵。我可以創建該值並將其保存到數據庫,但無法更新數據庫。 部分TaskEntity類:更新數據庫ManytoOne關係(Spring Boot + MVC + Thymeleaf)

@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類:

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

誰能幫助PLZ如何更新與控制器請求數據庫。提前致謝。

回答

0

請嘗試在行之後在您的控制器中添加updateTask方法中的這些行。

PhaseEntity phaseEntity=taskEntity.getPhaseEntity(); 
generaltaskDto.setTaskEntity(taskEntity); 
generaltaskDto.setPhaseId(phaseEntity.getPhaseId()); 
model.addAttribute("generaltaskDto", generaltaskDto); 
相關問題