Spring form controller: unable to save relations in form controller
I have three tables User, Role and UserRoleRelationships (many-to-many join table). In my service I have no problem editing the user and saving relating roles, but I'm not able to do this in the controller.
Expected behaviour in the service:
Hibernate: update dbo.Users set Username=?, Password=?, Email=?, Workphone=?, Privatephone=?, FullName=? where UserId=?
Hibernate: update dbo.UserRoles set Role=? where RoleId=?
Hibernate: update dbo.UserRoles set Role=? where RoleId=?
But the behaviour in the controller are:
Hibernate: update dbo.Users set Username=?, Password=?, Email=?, Workphone=?, Privatephone=?, FullName=? where UserId=?
Hibernate: delete from UserRoleRelationships where UserId=?
My controller looks like:
@RequestMapping(value = "/usermanagement/edit/{userId}")
public ModelAndView initUpdateForm(@PathVariable String userId, ModelMap model) {
model.addAttribute("user", iwUserManagementService.getUser(Integer.valueOf(userId)));
return new ModelAndView(viewName, model);
}
@RequestMapping(value = "/usermanagement/edit/{userId}", method = RequestMethod.POST)
public ModelAndView processUpdateSubmit(@ModelAttribute("user") IWUser iwUser,
BindingResult result, SessionStatus status){
iwUserManagementService.saveOrUpdate(iwUser);
status.setComplete();
return new ModelAndView("redirect:/usermanagement", new ModelMap("user", iwUser));
}
Part of my web.xml:
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>false</param-value>
</init-param>
<ini开发者_如何学编程t-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
For me it looks like the user model in processUpdateSubmit() does not return the roles, only the posted items. How can I make it return the roles as well?
Thanks :)
From the comments, it seams that the roles "disappears in the process function".
It seams that you have a problem with your form or the way the request is handled.
I my opinion you have two choices, depending on what you want to do:
If you do not want to change the users roles (in this form) then use an User Data Tansfer Object instead of the Role in
processUpdateSubmit
. Then you need only to load the user and update it with the data from the dto.If you want to change the user roles (in this form) then you need to find the bug:
- check that the form submit the roles
- check that the converters (role id to role) work correct
- check that spring can access the collection of roles in your user object.
- If everything looks ok, than set a break point a the
user.setRole()
method and go backward in the call stack.
I fixed it myself by adding @SessionAttributes(types = IWUser.class):
@Controller
@SessionAttributes(types = IWUser.class)
public class UserUpdateController { /* implemenation */ }
精彩评论