Announcement Announcement Module
Collapse
No announcement yet.
Partial edit a hibernate entity Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Partial edit a hibernate entity

    Please verify if I'm doing this correctly.

    Coming from Struts when I want to save a partially edited entity I would do the following. Use the "preparable" interface with a "prepare" method that would get the entity from the ID in request parameters. Then when the action handler method runs the correct Entity is available for request parameter injection and saving via hibernate.

    Is the following the CORRECT way to do the same in Spring MVC?

    Thanks,
    Ben

    Code:
    	@RequestMapping(method = RequestMethod.PUT, produces = "text/html")
        public String update(@ModelAttribute("user") @Valid MyUser myUser, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
            if (bindingResult.hasErrors()) {
                populateEditForm(uiModel, myUser);
                return "myusers/update";
            }
            uiModel.asMap().clear();
            myUser.merge();
            return "redirect:/myusers/" + encodeUrlPathSegment(myUser.getId().toString(), httpServletRequest);
        }
    	
    	@ModelAttribute("user")
    	private MyUser populateMyUserForEdit(@RequestParam(value = "id", required = false) Long id) {
    		if (id != null) {
    			return MyUser.findMyUser(id);
    		}
    		return null;
    	}
    	
    	@InitBinder
        protected void initBinder(WebDataBinder binder) {
    		binder.setDisallowedFields("id");
        }
Working...
X