Spring Data Jpa-同时更新两个实体会插入新值

时间:2019-01-21 12:22:34

标签: java spring-boot jpa

Database Structure

每当我尝试更新菜单和配料时,它只会在数据库中插入新值。

我检查并尝试仅更新 菜单实体,并且它起作用。

是否有“好方法”来更新多个实体类? (我选中了MenuingredientNameingredientDescription在用户进入更新页面时确实具有预定义的ID)

菜单实体:

@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

成分实体:

@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

Controller(仅Edit方法):

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

0 个答案:

没有答案