Hello!
I have problems saving values from checkboxes in the database. My jsp page contains several checkboxes (representing values from a static database table). When I choose one or a few checkboxes in my jsp, the values are passed to my SimpleFormController and they should be saved through the onSubmit method as a collection (ArrayList) in a link table. The values in the collection are correct, i.e. the controller gets only the values from the checked checkboxes.
Here is a part of my controller:
public ModelAndView onSubmit(
HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors)throws Exception
{
DailyProductionReport dpr = (DailyProductionReport)command;
List<Employee> employees = new ArrayList<Employee>();
String[] checks = request.getParameterValues("employee");
if(checks!=null){
for (int i = 0; i < checks.length; ++i){
int employee_id = Integer.valueOf(checks[i]);
System.out.println(employee_id); // employee IDs are those of employees selected
Employee employee = em.getEmployee(employee_id); // em - EmployeeManager
employees.add(employee); // so I add these employees to the list
employee = (Employee)employees.get(i);
System.out.println(employee.getSurname());
dpr.setEmployees(employees); // and I set the employees list as a property of my parent class DailyProductionReport
}
}
/*
* saving this DPR in the DB
*/
dprManager.addDpr(dpr);
return new ModelAndView(getSuccessView());
}
The problem is at dprManager.addDpr(dpr). The inserion is about to begin, but I get here an exception:
Hibernate: insert into DailyProductionReport (date, shift, tidiness, cleanness, dpr_id) values (?, ?, ?, ?, ?)
Hibernate: insert into DailyProductionReport_has_Employee (dpr_id, dprEmployee_id, employee_id) values (?, ?, ?)
12:55:11,640 ERROR DispatcherServlet:406 - Could not complete request
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
The relationship between DailyProductionReport and Employee is n-m with a link table DailyProductionReport_has_Employee, which should store the selected employees for the specified DailyProductionReport.
What can this excetpion mean in this case?
Why can't the insertion be completed?
snah1