Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7967761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:58:25+00:00 2026-06-04T06:58:25+00:00

What’s the best practice to pre-populate an object before saving this object with hibernate?

  • 0

What’s the best practice to pre-populate an object before saving this object with hibernate?

What i’ve done:

My controller:

//The Form
@RequestMapping(value = "user/{id}/edit", method = RequestMethod.GET)
public String edit(@PathVariable("id") Long userId, ModelMap modelMap) {
    modelMap.addAttribute("user", userService.find(userId));    
    return "user/userEdit";

}
//Updating database
@RequestMapping(value = "user/edit", method = RequestMethod.POST)
public String update(@ModelAttribute("user") @Valid User user, BindingResult result,
                                                RedirectAttributes redirectAttrs) {
    if (result.hasErrors()) {
            return "user/userEdit";
    }else{
        userService.update(user);
        redirectAttrs.addFlashAttribute("message", "Success");

        return "redirect:user/userEdit";
    }
}

It works if i make a form containing all fields (username, password and id) , but what should i do if i want the user to update only the password?

Since i have a @NotEmpty at username, i get an error that username is null, since its not in the form, but i dont want to put the username field, just the password.

My html form:

<c:url var="url" value="/user/edit" />
<form:form method="post" action="${url}" modelAttribute="user" class="form-horizontal">
    <form:hidden path="id"/>
    <form:hidden path="version"/>
    <fieldset>
        <div class="control-group">
            <form:label cssClass="control-label" path="password"><spring:message code="user.label.password"/>: </form:label>
            <div class="controls">
                <form:input cssClass="input-xlarge" path="password" />
            </div>
            <form:errors path="password"/>
        </div>
        <div class="control-group">
            <form:label cssClass="control-label" path="userRole"><spring:message code="user.label.role"/>: </form:label>
            <div class="controls">
                <form:select path="userRole">
                       <form:options items="${userRoleList}" itemValue="id" itemLabel="name"/>
                </form:select>
            </div>
            <form:errors path="userRole"/>
        </div>
        <div class="control-group">
            <form:label cssClass="control-label" path="costumer.id"><spring:message code="user.label.costumer"/>: </form:label>
            <div class="controls">
                <form:select path="costumer.id">
                       <form:options items="${costumerList}" itemValue="id" itemLabel="name"/>
                </form:select>
            </div>
            <form:errors path="costumer.id"/>
        </div>

        <div class="form-actions">
            <button type="submit" class="btn btn-primary">Save changes</button>
            <a class="btn cancel link" href="<c:url value="/user" />">Cancel</a>
        </div>
    </fieldset>
</form:form>
  • I tried using @Sessionattributes, but it doesnt work well if i try to
    edit two or more users using browser tabs.
  • I tried using property editors, but didnt work with @ModelAtrribute
    User user.
  • I tried using convertors but didnt work.

Is the only way to make a User user = userService.find(id) first, and then set the updated values? Something like:

@RequestMapping(value = "user/edit", method = RequestMethod.POST)
public String update(@RequestParam("password") String password, BindingResult result, RedirectAttributes redirectAttrs) {
    User user = userService.find(id);
if (password == null{                           
    return "user/userEdit";
}else{
    user.setPassword("password");
    userService.update(user);
    redirectAttrs.addFlashAttribute("message", "Success");

    return "redirect:user/userEdit";
}
}

Which seens wrong, because there is no validation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:58:26+00:00Added an answer on June 4, 2026 at 6:58 am

    An alternate way, that I think is less messy and accident-prone, is to create a class that models the UI form, for example

    public class EditUserForm {
        // getters and setters for password and other fields...
    }
    

    and in the controller’s update(EditUserForm,...) method, you simply map any fields populated by the user in the EditUserForm to the instance of User you wish to update.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.