I recently started working with JSP/SpringMVC/Hibernate.
and used equinox (https://equinox.dev.java.net/) as template for my application.
My Problem is that I can't manage to compare a new user password
entered in the jsp-view with the old one in the database.
I use my hibernate user model as command object for the view.
Following the approach of this post:
http://forum.springframework.org/showthread.php?t=39834
I started writing a custom property editor for the password handling and
get stuck while trying to access my userService. So I try to
access the old user password from my form controller with:
I get:Code:User userO = userService.getUser(request.getParameter("userId")); log.debug(userO);
"a different object with the same identifier value was already associated
with the session: [de.obs.model.User#14]; nested exception is
org.hibernate.NonUniqueObjectException: a different object with the
same identifier value was already associated with the session: [de.obs.model.User#14]"
I understand why this message occurs, but due to my lack of experience with
spring/hibernate and java in general, I can't figure out how to solve the problem.
UserFormController:
approach - CustomPasswordEditorCode:public class UserFormController extends SimpleFormController { private final Log log = LogFactory.getLog(UserFormController.class); private UserService userService = null; public void setUserService(UserService userService) { this.userService = userService; } public UserFormController() { setCommandName("user"); setCommandClass(User.class); } public ModelAndView processFormSubmission(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { if (request.getParameter("cancel") != null) { return new ModelAndView(getSuccessView()); } return super.processFormSubmission(request, response, command, errors); } protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) { ... // convert binder.registerCustomEditor(Set.class, "userRole", new CustomCollectionEditor(Set.class) { protected Object convertElement(Object element) { if (element != null) { Long roleId = new Long((String) element); Role userrole = userService.getRole(roleId); return userrole; } return null; } }); if (request.getParameter("userId") != null) { binder.registerCustomEditor(String.class, "password", new CustomPasswordEditor()); } } protected Map referenceData(HttpServletRequest request) { Map<String,Object> data = new HashMap<String,Object>(); java.util.List roles = userService.getAllRoles(); data.put("roles", roles); return data; } public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { User user = (User) command; if (request.getParameter("delete") != null) { userService.removeUser(user.getUserId().toString()); request.getSession().setAttribute("message", getText("user.deleted", user.getFullName())); } else { userService.saveUser(user); request.getSession().setAttribute("message", getText("user.saved", user.getFullName())); } return new ModelAndView(getSuccessView()); } protected Object formBackingObject(HttpServletRequest request) throws ServletException { String userId = request.getParameter("userId"); if ((userId != null) && request.getMethod().equalsIgnoreCase("get")) { User user = userService.getUser(userId); //user.setPassword(""); return user; } else { return new User(); } } ... }
Code:public class CustomPasswordEditor extends PropertyEditorSupport { private final Log log = LogFactory.getLog(CustomPasswordEditor.class); private String userId = null; public CustomPasswordEditor(){}; public CustomPasswordEditor(String userId){ this.userId = userId; } public void setAsText(String formPassword) { // get old password + setValue("processed password"); } }


Reply With Quote