Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the "correct" or "safe" way to update a persisted object with Spring MVC 3 + Hibernate?
    primarykey
    data
    text
    <p>Given a very simple object:</p> <pre><code>class User { private Integer id; private String name; public User() {} public Integer getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } </code></pre> <p>and a very simple controller action:</p> <pre><code>@RequestMapping(value="/edit/{id}/**", method=RequestMethod.POST) public String editFromForm(@PathVariable("id") Integer id, @Valid User user, BindingResult bindingResult, Model model) { // If we have errors, don't save if(bindingResult.hasErrors()) { // Put what they did in the model and send it back model.addAttribute(user); return "users/edit"; } else { userDAO.save(user); } // Show them the updated page on success return "redirect:/users/" + user.getId() + "/" + user.getName(); } </code></pre> <p>and a very simple form:</p> <pre><code>&lt;sf:form method="POST" modelAttribute="user"&gt; &lt;label for="user_name"&gt;Name:&lt;/label&gt; &lt;sf:input path="name" id="user_name" /&gt; &lt;input type="submit" value="save" /&gt;&lt;sf:errors path="name" cssClass="error" /&gt; &lt;/sf:form&gt; </code></pre> <p>How <em>should</em> I be updating the entity in the database? Currently (since <code>saveOrUpdate()</code> is the actual hibernate call behind my DAO's <code>save()</code> method, a new object is persisted instead of updating the existing one because the <code>id</code> field is not being set on the object created from the form submission.</p> <p>A couple of possible solutions have come to me, but I am not sure which is best in terms of keeping things clean and also secure (so that a malicious user cannot just fire in edits to whatever object Id's they wish).</p> <ol> <li>Insert the id from the URL parameter into the object coming from the model binder</li> <li>Have a hidden <code>id</code> field in the form and let the model binder attach the id</li> </ol> <p>In both of those cases, there is no check in place to make sure that the object is still the same one, such as a checksum of some sort. How do others deal with this? Are there any clear example that walk through this issue?</p> <p>Another issue that comes up is that I'd rather not need a <code>setId()</code> method since Hibernate is managing all of the id's. From what I have been able to determine, the Spring MVC model binder can only bind a field if it has the expected getter and setter. Is there some other way to apply the new state, such as getting the current User from the db by the URL's <code>id</code> parameter and then applying the new state to it, but without having to explicitly code all of the field copies?</p> <p>I am sure there is a fairly simple, straightforward way to handle this, but my heat-addled brain does not seem to be able to come up with a suitable solution.</p> <p>I'm fairly new to Spring + Hibernate so forgive me if this is one of those mundane, highly covered topics, but I have not been able to find any clear example that covers my very simple situation. If this <em>has</em> been covered sufficiently elsewhere, please point me in the right direction.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload