Best way to modify (update) an object with Spring and Hibernate in this scenario?


I do not know how to move forward with this, to see if someone gives me a hand:)

In the modal that I see in the image several loaded credentials will appear. Each credential can be edited or deleted and I'm having architectural problems to do the edit (I'm new with Spring and Hibernate).

When clicking on edit I made the top modal replaced with a new one with 3 inputs to make the edit:

... from here I do not know how I could go and that's where I need help.

What I did was this:

// Editar credencial de un FisicHost
$("#credentialsTable").on('click',"button[id^='edi-']",  (e) => {

    var credentialId =;
    var newUser = $('#newUser').val();
    var newPass = $('#newPass').val();
    var newNotes = $('#newNotes').val();

    $.post( "/fisicHost/" + credentialId + "/credentials/update", data => {

It occurred to me to take the value of the 3 inputs and then make a post to the controller with those 3 values + the credential ID to edit ...

// Método para actualizar credenciales
@RequestMapping(value = "/fisicHost/{id}/credentials/update", method = RequestMethod.POST)
public void updateCredential(@PathVariable(value = "id") String credId){
    String[] parts = credId.split("-");
    int id = Integer.parseInt(parts[1]);
    Credential credential = credentialService.getCredentialById(id);

From here I get questions like:

How should I send those 3 values of the 3 inputs? If you look at the post that I do, in the URL I'm only sending the ID of the credential I want to update, I do not know how to send those 3 values of inputs to validate them in the controller (and I do not want to send them in the URL as with ID of the credential)!

And second ... once received by the controller and made the validations, how do I return the updated data of the controller to the view ??

Any help, explanation will be more than welcome.

Thank you very much everyone

asked by Nacho Zve De La Torre 03.12.2018 в 15:00

1 answer


When you do a POST or a PUT, it is usual to send the data in the body of the call:

let credentialData = {
  dato1: ...,
  dato2: ...

$.post(url, credentialData).then(respuesta => {

Since the data is passed in the body, you should wait for them in the backend in the body:

@PostMapping(value = "/fisicHost/{id}/credentials/update")
public ResponseEntity<Credential> updateCredential(@RequestBody Credential credential) {
    return ResponseEntity.ok(respuesta); //respuesta es de tipo Credential

This means that you would have to create a bean class (simply with getters and setters) equivalent to the javascript object you are going to send, so that Spring will fill it in automatically.

That class can be used both to receive the data and to send it in the answer

answered by 03.12.2018 в 18:05