在Spring Framework中使用休眠的OneToMany映射

时间:2019-09-23 15:33:43

标签: hibernate

我在Spring较新鲜,请帮助我。 我有两个实体Customer和CustomerHobbies,它们根据hibernate进行了正确映射(没有问题),在将客户对象保存到saveCustomer方法中时,它抛出了一个我完全理解的可怕异常:

无法将类型java.lang.String []的属性值转换为属性listOfHobbies的必需类型java.util.List;嵌套异常是java.lang.IllegalStateException:无法将类型java.lang.String的值转换为属性listOfHobbies [0]的必需类型com.luv2code.springdemo.entity.CustomerHobbies:找不到匹配的编辑器或转换策略 < / p>

我的问题是如何克服这个问题? JSP页面编码:

<form:select path="listOfHobbies" multiple="true">              
                <form:option value="Cricket" label="Cricket"/>
                <form:option value="Hockey" label="hockey"/>
                <form:option value="Snooker" label="Snooker"/>
            </form:select>

控制器代码:

@RequestMapping("/saveCustomer")
    public String saveCustomer(@Valid @ModelAttribute("addCustomer") Customer theCustomer,
            BindingResult theBindingResult, Model CustomerModel) {

        if (theBindingResult.hasErrors()) {
            System.out.println("Has errors :"+theBindingResult);
            // getting the customers from customerService instead
            List<Customer> custList = customerService.getCustomers();

            // add departments for select box
            LinkedHashMap<Integer,String> hash= customerService.getDeptForSelectBox();
            CustomerModel.addAttribute("departmentList", hash);

            // add those customers to the Modal
            CustomerModel.addAttribute("customerList", custList);
            return "manage-customers";
        } 
        else {
            System.out.println("No errors");
            // save the customer to DB
            System.out.println("Customer hobbies :"+theCustomer.getListOfHobbies().size());
            customerService.saveCustomer(theCustomer);
            return "redirect:/customer/manageCustomer"; 
            // cz we are redirecting to controller of jsp view page and not jsp page itself
        }

    }

客户实体代码:

@OneToMany(mappedBy="customer",cascade=CascadeType.ALL)
    private List<CustomerHobbies> listOfHobbies= new ArrayList<CustomerHobbies>();

CustomerHobbies实体代码:

@ManyToOne
    @JoinColumn(name="customerId")
    private Customer customer;

Front End screenshot

0 个答案:

没有答案