Im looping over two lists and want to add the objects to a custom object. So Im getting an Employee
a list of Projects
and a list of Doubles
from Thymeleaf and save them to a database. I want to save them to the object EmployeeProject
which takes an Employee, a Project and a double (employeeBookedMonths). My problem is I either get the multiple projects saved to the EmployeeProject but all the project months are the same (the last double value one in the list) or I save the multiple projects to the employeeProject with correct employeeBookedMonths but there is dupicates, for example if I save 3 projects with 3 employeeBookedMonths, I get 3 times each saved (so 9 saved). I've tried moving the employeeProjectService.saveEmployeeProject
but can't make it work plus many other variations but I need some help.
Question: how can I loop through the 2 lists and add and them to the EmployeeProject object? Without getting multiples or only getting the last employeeBookedMonths.
Thanks in advance.
Also the System.println
prints the wanted values, they just don't save.
public String saveEmployee(@ModelAttribute("employee") Employee employee,
@RequestParam("projectId") List<Project> projectIds,
@RequestParam("employeeProjectMonths") List<Double> months) {
List<Double> monthList = new ArrayList<>();
for (Double month : months) {
if (month != null) {
monthList.add(month);
System.out.println("Month:" month);
}
}
employeeService.saveEmployee(employee);
if (projectIds != null) {
EmployeeProject employeeProject = new EmployeeProject(employee);
for (Project ids : projectIds) {
for (Double month : monthList) {
employeeProject.setEmployeeBookedMonths(month);
System.out.println("Months: " employeeProject.getEmployeeBookedMonths());
employeeProjectService.saveEmployeeProject(employee, ids, month);
}
}
}
return "redirect:/ines/employees";
}
New Employee form
<form action="#" th:action="@{/ines/saveEmployeeMeeting}" th:object="${employee}"
method="POST" enctype="multipart/form-data">
<div >
<label>Email:</label>
<input type="text" th:field="*{name}"
placeholder="Employee Name" >
</div>
<th:block th:object="${meetingInfo}">
<div >
<label>Start Time:</label>
<div id="datetimepicker1" data-target-input="nearest">
<input type="text"
th:field="*{meetingStartDateTime}" id="meetingStartDateTime"/>
<span >
<span ></span>
</span>
</div>
</div>
<div >
<label>End Time:</label>
<div id="datetimepicker2" data-target-input="nearest">
<input type="text" data-target="#datetimepicker1"
th:field="*{meetingEndDateTime}" id="meetingEndDateTime" placeholder="Date"/>
<span >
<span ></span>
</span>
</div>
</div>
<div >
<input type="text" th:field="*{message}"
placeholder="Message" >
</div>
</th:block>
<button type="submit" >Save Employee/Meeting</button>
</form>
CodePudding user response:
In this line:
employeeProjectService.saveEmployeeProject(employee, ids, month);
it was supposed to save employee
or employeeProject
?
Because in the code you didn't use the variable employeeProject
explicitly. Where it was supposed to be saved?
CodePudding user response:
Did you consider that you're declaring EmployeeProject employeeProject = new EmployeeProject(employee);
inside if (projectIds != null) {
scope, so if you do println in the employeeProject
, right above return "redirect:/ines/employees";
(outside the scope) you won't see it? In order to don't loose the data you need to declare employeeProject
at the beginning of the method.
The follow code could correct this:
public String saveEmployee(@ModelAttribute("employee") Employee employee,
@RequestParam("projectId") List<Project> projectIds,
@RequestParam("employeeProjectMonths") List<Double> months) {
List<EmployeeProject> listOfEmployeeProjects = ArrayList<>();
List<Double> monthList = new ArrayList<>();
for (Double month : months) {
if (month != null) {
monthList.add(month);
System.out.println("Month:" month);
}
}
employeeService.saveEmployee(employee);
if (projectIds != null) {
EmployeeProject employeeProject = new EmployeeProject(employee);
for (Project ids : projectIds) {
for (Double month : monthList) {
employeeProject.setEmployeeBookedMonths(month);
System.out.println("Months: " employeeProject.getEmployeeBookedMonths());
employeeProjectService.saveEmployeeProject(employee, ids, month);
}
}
listOfEmployeeProjects.add(employeeProject);
}
//Here you might see the data inside listOfEmployeeProjects, once it was declared in a outter scope
//Maybe do a assign of your data to other field, for example:
// mainData.setAllOfTheEmployeeProjects(listOfEmployeeProjects);
return "redirect:/ines/employees";
}
Did it help? If didn't please post the rest of your code.