Spring Data Jpa - при обновлении двух объектов вместе добавляется новое значение - PullRequest
0 голосов
/ 21 января 2019

Database Structure

Всякий раз, когда я пытаюсь обновить меню и ингредиенты, он просто вставляет новые значения в базу данных.

Я проверил и попытался обновить только сущность Menu, и это сработало.

Есть ли "хороший способ" обновить несколько классов сущностей?(Я проверил, и Menu и ingredientName, ingredientDescription имеют предопределенные идентификаторы, когда пользователь переходит на страницу обновления)

Элемент меню:

@Entity
@Table(name = "menu")
public class Menu {

    @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
@Table(name = "ingredients")
public class Ingredients {

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

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

    @Column(name = "description")
    private String ingredientDescription;
    //Getters/Setters/Constructors/ToString

Контроллер (только методы редактирования):

@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);
            // To Check data in the List , the output is : 
            // Ingredients{id=70, ingredientName='pizzaing1', ingredientDescription='pizzadesc1'}
        }

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


        return "edit-page";
    }

    @PostMapping("/postEditMenu")
    public String postEdit(@ModelAttribute("newMenu")Menu menu,
                           @RequestParam(name = "ingName")String ingName,
                           @RequestParam(name = "ingDesc")String ingDesc){



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

    for(int a = 0, b = 0; a<ingNameSplit.length; b++, a++){
        menu.getIngredient().add(new Ingredients(ingNameSplit[a], ingDescSplit[b]));
    }
        menuRepository.save(menu);

        return "redirect:/recipeList";
    }

Страница редактирования:

<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>

MenuRepository просто расширяется JpaRepository<Menu,String>

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...