我终于弄明白了。即使保存方法是无效的,您也可以为它设置一个变量。(?)
@PostMapping(value = "customer/{id}/wish/add")
public String processAddWish(Model model, @PathVariable int id,
@ModelAttribute @Valid WishOrder newWishOrder,
Errors errors) {
if (errors.hasErrors()) {
return "customer/wish/add";
}
//Initialize variable to the save method, even though it is void and returns nothing//
//Eliminates need to access object through a .findById//
//If you need the id of the saved object instantly, just add .getId to save call//
//wishOrderDao.save(newWishOrder).getId()//
WishOrder wish = wishOrderDao.save(newWishOrder);
Customer customer = customerDao.findById(id);
customer.getWishList().add(wish);
customerDao.save(customer);
model.addAttribute("customer",(customer));
model.addAttribute("wishList", (customer.getWishList()));
return "customer/wishList";
}