BindException during binding data in form

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

BindException during binding data in form

slayer9
Controller looks like this:

    @RequestMapping("/product/new")<nabble_embed></nabble_embed>
    public String newProduct(Model model) {
        model.addAttribute("cat", categoryService.getAllCategoriesCommand());
        model.addAttribute("productf", new ProductCommand());
        return "product/addProduct";
    }

     @PostMapping("product")
    public String saveNewProduct(@ModelAttribute("productf") ProductCommand productCommand, Model model) {
        productService.saveCommand(productCommand);
        return "redirect:/index";
    }

Form:
          <tr th:each="category : ${cat}">
            <div class="radio">
                <label>
                    <input type="checkbox" value="" th:value="${category.id}" th:text="${category.categoryName}" th:field="*{categories}"/>
                </label>
            </div>

Domain/Command classes:
    @Entity
    @EqualsAndHashCode(exclude = "categories")
    public class Product {
 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String description;
    private BigDecimal price;
    private String manufacturer;
    private Long unitsInStock;
    private boolean discontinued;
    private Byte[] image;
 
    //@ElementCollection
    //private Map<String, String> productProperties = new HashMap<>();
 
    @ManyToMany(cascade = CascadeType.PERSIST)
    @JoinTable(name = "product_category",
            joinColumns = @JoinColumn(name = "product_id"),
    inverseJoinColumns = @JoinColumn(name = "category_id"))
    private Set<Category> categories = new HashSet<>();
    }

ProductCommand class:

   
@NoArgsConstructor
    public class ProductCommand {
 
    private Long id;
    private String name;
    private String description;
    private BigDecimal price;
    private String manufacturer;
    private Long unitsInStock;
    private boolean discontinued;
    private Byte[] image;
    private Set<CategoryCommand> categories = new HashSet<>();
    }

Category class:

   
 @Entity
    @EqualsAndHashCode(exclude = "products")
    public class Category  {
 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String categoryName;
 
    @ManyToMany(mappedBy = "categories")
    private Set<Product> products; 
    }

CategoryCommand class:

    @NoArgsConstructor
    public class CategoryCommand {
 
    private Long id;
    private String categoryName;
    }

And when I want submit the form I got below error:

Resolved exception caused by Handler execution: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'productf' on field 'categories': rejected value [1]; codes [typeMismatch.productf.categories,typeMismatch.categories,typeMismatch.java.util.Set,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [productf.categories,categories]; arguments []; default message [categories]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Set' for property 'categories'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'info.mike.webstorev1.commands.CategoryCommand' for property 'categories[0]': no matching editors or conversion strategy found]


I don't know why Spring cannot convert String, I don't see any Strings there. And when I (in controller) pass to to the model New Product and categoryService.getAllCategories() instead of New ProductCommand and categoryService.getAllCategoriesCommand() it all works very well with no exceptions. I am asking for help.