How to update record in springboot JpaRepository?
<p>My model is </p>

<pre><code>@Entity
@Table(name = "user")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name="name")
private String name;

@Column(name="email")
private String email;   

//getter &amp; setter
}
</code></pre>

<p>My controller is</p>

<pre><code>@RequestMapping(value = "user/userProfile", method = RequestMethod.POST)

public String saveUserProfile(@Valid UserProfile userProfile, @RequestParam("file") MultipartFile file,
        BindingResult bindingResult, Model model) {
    if (bindingResult.hasErrors()) {
        model.addAttribute("userProfile", userProfile);
        return "userprofileform";
    }           
    userProfileService.saveUserProfile(userProfile);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "redirect:/user/home";
}
@RequestMapping("/user/userProfile/new")
public String newUserProfile(Model model) {
       UserProfile profile = userProfileService.getUserProfileByUserId(userService.getLoggedInUser().getId());
    model.addAttribute("userProfile", profile);
    return "userprofile";
}
</code></pre>

<p>Repository is </p>

<pre><code>@Transactional
public interface UserProfileRepository extends CrudRepository&lt;UserProfile, Long&gt;{

}
</code></pre>

<p>When I am creating first time userProfile it creates successfully but when I tried to update same it create new entry.</p>

<p>But in this controller when I use findone() and copy all information it works fine.</p>

<p>Same save method replaced below like :</p>

<pre><code>public String saveUserProfile(@Valid UserProfile userProfile, @RequestParam("file") MultipartFile file,
        BindingResult bindingResult, Model model) {
    if (bindingResult.hasErrors()) {
        model.addAttribute("userProfile", userProfile);
        return "userprofileform";
    }           
    UserProfile tempProfile = userProfileService.getUserProfileByUserId(userService.getLoggedInUser().getId());
    try {                               
        if (tempProfile != null) {
            tempProfile.setName(userProfile.getName());              
            tempProfile.setEmail(userProfile.getEmail());                               userProfileService.saveUserProfile(tempProfile);
        } else {
            userProfile.setUserId(userService.getLoggedInUser().getId());
            userProfileService.saveUserProfile(userProfile);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "redirect:/user/home";
}
</code></pre>

<p>Is this write way or how can we update record?.</p>

<p>Or saving in session attribute (@SessionAttributes("yourAttributeName")) is only the write way?</p>