Q: Spring Data JPA problem with updating multiple Entities I have 2 Entity classes the "Menu" which only has one field called "name" and second Entity - "Ingredients" which has 2 fields - "ingredientName" and "ingredientDescription". Database Structure 
I'm creating a simple CRUD web-app , but the update method instead of updating the Entity , it inserts new values in the DB. I checked and when user clicks on the update on specified menu, the first entity's id and its ingredients id's as well are predifined. Im new to spring boot and thymeleaf and Don't really know how to work with JPA when you have more than 1 entity.

Menu entity : 

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;

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

// Mapping To second table
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinTable(name = "menu_ingredient",
        joinColumns = @JoinColumn(name = "menu_id"),
        inverseJoinColumns = @JoinColumn(name = "ingredient_id"))
private List<Ingredients> ingredient = new ArrayList<>();

//Getters/Setters/Constructors/ToString


Ingredients entity : 

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

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

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

//Getters/Setters/Constructors/ToString


Controller(Only the update methods) : 

@GetMapping("/edit/{id}")
public String edit(@PathVariable(name = "id")String id, Model model){
    Optional<Menu> menu = menuRepository.findById(id);

    List<Ingredients> ingredients = menu.get().getIngredient();

    for (Ingredients ing : ingredients){
        System.out.println(ing);
    }

    model.addAttribute("ingredients", ingredients);
    model.addAttribute("newMenu",menu);


    return "edit-page";
}

@PostMapping("/postEditMenu")
public String postEdit(@ModelAttribute("newMenu")Menu menu){



    menuRepository.save(menu);

    return "redirect:/recipeList";
}


edit-page.html :

    <form action = "#" th:action="@{/postEditMenu}" th:object="${newMenu}" method="post">
        <p>Menu Name: <br><input type="text" th:field="*{name}"></p>
        <div id="wrapper" th:each="ing: ${ingredients}">
            <label for="ingredientName"></label>
            <p>Ingredient Name: <br><input th:value="${ing.ingredientName}" id="ingredientName" type="text" name="ingName"></p>

            <label for="ingredientDescription"></label>
            <p>Ingredient Description:</p> <textarea id="ingredientDescription" type="text" th:text="${ing.ingredientDescription}" name="ingDesc"></textarea>
        </div>
        <br>

        <input type="button" id="more_fields" onclick="add_fields();" value="Add More" />
        <br>

        <input type="submit" th:value="Submit">

    </form>

FIX I actually figured it out with the help of below answers. Here's the code : 
@PostMapping("/postEditMenu")
public String postEdit(@ModelAttribute("newMenu")Menu menu,
                       @RequestParam String ingName,
                       @RequestParam String ingDesc){

    String[] ingNameSplit = ingName.split(",");
    String[] ingDescSplit = ingDesc.split(",");

    Menu menuToUpdate = menuRepository.getOne(menu.getId());


    List<Ingredients> newIngredientList = menuToUpdate.getIngredient();
    newIngredientList.clear();

    for(int a = 0, b = 0; a<ingNameSplit.length; b++, a++){
        newIngredientList.add(new Ingredients(ingNameSplit[a], ingDescSplit[b]));
    }
    menuToUpdate.setIngredient(newIngredientList);

    menuRepository.save(menuToUpdate);

    return "redirect:/recipeList";
}

So, First I added hidden "id" fields to each of the items required , like this : 
<input type="text" th:field = "*{id}" hidden>

and 
<input type="text" th:value = "${ing.id}" hidden>

Then, in the postEditMenu method, I added @RequestParam String ingName, and @RequestParam String ingDesc to get the input of new items from thymeleaf, then I split that String and add it to String[] array with String[] ingNameSplit = ingName.split(",") Because the input would be one big comma separated String and not array[] . Then I get the menu which user wants to update - Menu menuToUpdate = menuRepository.getOne(menu.getId()); The menu.getId() isn't null because I set hidden "id" fields in thymeleaf. Then I get the Ingredients of this menu - List<Ingredients> newIngredientList = menuToUpdate.getIngredient(); because the list would already be filled with existed ingredients I clear that list and add new ingredients which user will fill the form with -  
for(int a = 0, b = 0; a<ingNameSplit.length; b++, a++){
            newIngredientList.add(new Ingredients(ingNameSplit[a], ingDescSplit[b]));
        }

after that I set this newIngredientsList and save the menu itself to the db - 
menuToUpdate.setIngredient(newIngredientList);

menuRepository.save(menuToUpdate);

Thanks for all the help guys :)

A: First you need to fetch Menu entity from database by id using getOne and then you can update it. 
Edit your code in postEdit method as follows:
Fetch Menu entity:
Menu menuToUpdate = menuRepository.getOne(menu.getId());

Update attributes:
menuToUpdate.setName(menu.getName());
Save entity:
menuRepository.save(menuToUpdate); 


A: At this point:
@PostMapping("/postEditMenu")
public String postEdit(@ModelAttribute("newMenu")Menu menu){


menuRepository.save(menu);

return "redirect:/recipeList";
}

You receive menu from edit-page.html and it has no id, that is why it always creates new records in database. 
To edit the desired menu, you would need to have it's id before. 
You can create endpoint for obtaining list of menus and display them with edit button next to each menu in html site. Then if user clicks edit button redirect him to your edit form, but this time you can pass menu's id. 

A: Add a hidden id field to hold the menu's id, and add hidden id fields for each ingredient.
