【发布时间】:2015-03-12 20:17:42
【问题描述】:
我有一个有很多列的表。当我使用@ModelAttribute 更新某些列时,jsp 页面中未指定的列中的数据将被删除。
例子
@RequestMapping(value= "/studenteducation", method= RequestMethod.GET)
public String setupUpdateEducationForm(Model model, @ModelAttribute("education") Student student){
student = studentService.getStudent(getStudentName());
model.addAttribute("education", student); //adding saved student data into model
return "studenteducation";
}
@RequestMapping(value= "/studenteducation", method= RequestMethod.POST)
public String studentEducationForm(Model model, @ModelAttribute("education") Student student, BindingResult result){
if(result.hasErrors()){
return "studenteducation";
}
student.setStudentId(getStudentName()); // inserting primary key
studentService.updateStudent(student); // updating student table
return "redirect:/";
}
问题是我没有绑定此更新中的所有列,因此只留下绑定的列数据,其他列数据从数据库中删除。我有 15 列,我在这个 jsp 页面中绑定了 5 列。
我的完整控制器代码:
@Controller
@RequestMapping("/user")
@SessionAttributes("education")
public class StudentController {
@Autowired
StudentService studentService;
Student student = new Student();
// Setup new Student form------------------------------------------------------------------
@RequestMapping(value= "/newuser", method= RequestMethod.GET)
public String setupAddStudentForm(Model model, @ModelAttribute("student") Student student){
return "registerstudent";
}
// Add new Student------------------------------------------------------------------
@RequestMapping(value= "/newuser", method= RequestMethod.POST)
public String sddStudent(Model model, @ModelAttribute("student") Student student, BindingResult result, SessionStatus sessionStatus){
if(result.hasErrors()){
return "registerstudent";
}
studentService.addStudent(student);
sessionStatus.setComplete();
return "redirect:/";
}
// Setup Edit Profile Form------------------------------------------------------------------
@RequestMapping(value= "/updateprofile", method= RequestMethod.GET)
public String setupStudentProfileForm(Model model, @ModelAttribute("profile") Student student ){
Student stu = studentService.getStudent(getStudentName());
model.addAttribute("name", student.getStudentId());
model.addAttribute("studentprofile", stu);
return "studentprofile";
}
// Update student profile------------------------------------------------------------------
@RequestMapping(value= "/updateprofile", method= RequestMethod.POST)
public String studentProfileForm(Model model, @ModelAttribute("profile") Student student, BindingResult result, SessionStatus sessionStatus ){
if(result.hasErrors()){
return "studentprofile";
}
student.setStudentId(getStudentName());
studentService.updateStudent(student);
sessionStatus.setComplete();
return "redirect:/";
}
// Setup Update Education Form------------------------------------------------------------------
@RequestMapping(value= "/studenteducation", method= RequestMethod.GET)
public String setupUpdateEducationForm(Model model, @ModelAttribute("education") Student student){
student = studentService.getStudent(getStudentName());
model.addAttribute("education", student);
return "studenteducation";
}
// Update Student Education------------------------------------------------------------------
@RequestMapping(value= "/studenteducation", method= RequestMethod.POST)
public String studentEducationForm(Model model, @ModelAttribute("education") Student student, BindingResult result, SessionStatus sessionStatus){
if(result.hasErrors()){
return "studenteducation";
}
student.setStudentId(getStudentName());
studentService.updateStudent(student);
sessionStatus.setComplete();
return "redirect:/";
}
我的学生模型 id 很大,我正在通过 2 个控制器对其进行更新。不同jsp页面中的学生资料和具有不同控制器的不同jsp页面中的教育。当我更新一部分时,另一部分会删除。
【问题讨论】:
-
桌上有触发器吗?
-
不,它只是带有休眠注释@Entity 的简单表。
-
有没有其他更好的方法来更新 hibernate 中的表。我正在使用 getSessionFactory.update(student)
标签: java hibernate jsp spring-mvc