【问题标题】:How to send an object with model including its child object如何发送带有模型的对象,包括其子对象
【发布时间】:2020-07-13 12:59:24
【问题描述】:

我想添加新的产品对象及其类别。产品对象已成功发送,但其类别(产品内部)为空。该类别无法发送的错误是什么。我该如何解决? 代码在@Controller.

@GetMapping("/loadForm")
public String addNewProduct(Model model){
    Product theProduct = new Product();
    List<Category> categories = (List<Category>) categoryService.findAll();
    model.addAttribute("categories", categories);
    model.addAttribute("product", theProduct);
    return "addProduct";
}
@PostMapping(value="/add")
public String addProduct(@ModelAttribute("product") Product product) {
    System.out.println("Checking");
    productService.insert(product);
    return "product";
}

HTML 文件中的代码是

<form th:action="@{/products/add}" th:object="${product}" method="POST">

    <input type="hidden" value="100" th:field="*{productId}"/><br>

        <input type="text" th:field="*{barcode}"
           class="form-control mb-4 col-10" placeholder="barcode"><br><br>

    <input type="text" th:field="*{createdUser}"
           class="form-control mb-4 col-10" placeholder="created User"><br><br>

    <input type="text" th:field="*{lastModifiedUser}"
           class="form-control mb-4 col-10" placeholder="last Modified User"><br><br>

    <input type="text" th:field="*{productIsService}"
           class="form-control mb-4 col-10" placeholder="product Is Service"><br><br>

    <input type="text" th:field="*{productName}"
           class="form-control mb-4 col-10" placeholder="product name"><br><br>

    <input type="text" th:field="*{productbuyingPrice}"
           class="form-control mb-4 col-10" placeholder="product buying Price"><br><br>

    <input type="text" th:field="*{productsellingPrice}"
           class="form-control mb-4 col-10" placeholder="product selling Price"><br><br>

    <input type="text" th:field="*{version}"
           class="form-control mb-4 col-10" placeholder="version"><br><br>

    <select th:field="*{category}"
            class="form-control mb-4 col-10" placeholder="Select category">
        <option th:each="tempCategory : ${categories}" th:value="${tempCategory}" th:text="${tempCategory.categoryName}"></option>
    </select>

    <button type="submit" class="btn btn-info col-4">Save</button>

</form>

产品和类别之间的关系是多对一的。产品类中的代码

@ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE,
            CascadeType.PERSIST,CascadeType.REFRESH})
    @JoinColumn(name="category_id")
    private Category category;

类别类中的代码

@JsonIgnore
    @OneToMany(mappedBy="category")
    private List<Stock> stocks;

【问题讨论】:

    标签: java spring web thymeleaf


    【解决方案1】:

    在您的场景中,一种产品将有多个类别。 映射应该是oneToMany

    在产品类中:

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name="category_id") 
    Set<Category> categories;
    

    在类别类中:

    @JoinColumn(name = "category_id")
    @ManyToOne(cascade = CascadeType.ALL)
    private Product product;
    

    【讨论】:

    • 感谢您的快速回复。
    【解决方案2】:

    我只是将cascadefetch 添加到类别ID 中。我替换这个:

    @JsonIgnore
        @OneToMany(mappedBy="category")
        private List<Stock> stocks;
    

    有了这个:

    @OneToMany(fetch = FetchType.LAZY,
                mappedBy="category",
                cascade = {CascadeType.DETACH, CascadeType.MERGE,
                        CascadeType.PERSIST, CascadeType.REFRESH})
        public List<Product> products;
    

    子实体中缺少 Cascade。

    【讨论】:

      猜你喜欢
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      • 2016-11-09
      • 1970-01-01
      • 2013-08-31
      • 2013-04-15
      • 1970-01-01
      相关资源
      最近更新 更多