I got two entities Employee.class
and Project.class
with @ManyToMany
relation between them.
When I'm trying to update Employee
and add another Project
to it via JSP form, controller makes Set<Project>
empty. So, when I submit the form, hibernate instead of adding another project to Set<Project>
, makes this:
Hibernate: update employee set ....(IT'S OK)
Hibernate: update project set...(IT'S OK)
Hibernate: delete from employee_project where employee_id=?(IT'S NOT OK)
Employee.class
@Entity
@Table(name="employee")
public class Employee{
//other fields
@ManyToMany(fetch= FetchType.EAGER,
cascade=CascadeType.ALL)
@JoinTable(name="employee_project",joinColumns=@JoinColumn(name="employee_id"),
inverseJoinColumns=@JoinColumn(name="project_id"))
private Set<Project> projects = new HashSet<>();
public Set<Project> getProjects() {
return projects;
}
public void setProjects(Set<Project> projects) {
this.projects = projects;
}
Project.class
@Entity
@Table(name="project")
public class Project {
@ManyToMany(fetch= FetchType.EAGER, mappedBy="projects")
private Set<Employee> employees = new HashSet<>();
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
EmployeeController.class
@GetMapping("/employeeForm")
public String showEmployeeForm(Model theModel) {
Employee theEmployee = new Employee();
theModel.addAttribute("employee", theEmployee);
theModel.addAttribute("projects", projectService.getProjectsIdMap());
return "employee-form";
}
@GetMapping("/employee/{id}/updateEmployee")
public String updateEmployee(@PathVariable("id") Long employeeId, Model theModel) {
//HERE MY Set<Project> IS FULL OF PROJECTS
theModel.addAttribute("employee", employeeService.getEmployee(employeeId));
theModel.addAttribute("projects", projectService.getProjectsIdMap());
return "employee-form";
}
@PostMapping("/saveEmployee")
public String saveOrUpdateEmployee(@ModelAttribute("employee")
Employee theEmployee) {
//HERE MY Set<Project> IS EMPTY
theEmployee.getProjects()
.add(projectService
.getProject(theEmployee.getProjectId()));
employeeService.saveEmployee(theEmployee);
return "redirect:/employees/employee/" + theEmployee.getId();
}
employee-form.jsp
<form:form action="/employee_manager/employees/saveEmployee"
modelAttribute="employee" method="POST">
<form:hidden path="id"/>
<form:select path="projectId">
<form:options items="${projects}" />
</form:select>
<input type="submit" value="Save" />
</div>
</form:form>
Employee.Repository (DAO)
public void saveEmployee(Employee employee) {
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(employee);
}
Employee.Service
@Transactional
public void saveEmployee(Employee employee) {
employeeRepository.saveEmployee(employee);
}
DB structure: enter image description here
It looks like somewhere spring set a new collection instead of adding to existing one.
I was trying to debug the app flow and follow Set<Projects>
and I know that this collection losess all values in saveOrUpdateEmployee
method. I can't figure out where did I make mistake. Thanks for your help.