我有候选和空缺实体(多对多注释)。
在候选映射中:
@ManyToMany(mappedBy = "candidates", fetch = FetchType.EAGER)
@XmlTransient
public Set<Vacancy> getVacancies() {
return vacancies;
}
在空缺映射中:
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "vacancy_candidate", joinColumns = @JoinColumn(name = "vacancy_id"), inverseJoinColumns = @JoinColumn(name = "candidate_id"))
public Set<Candidate> getCandidates() {
return candidates;
}
调用代码:
@RequestMapping("/saveCandidate")
public String saveCandidate(Model model,HttpServletRequest request,HttpResponse response
,@ModelAttribute("candidateFromRequest") Candidate candidateFromRequest
,@ModelAttribute("skillsIdList") Set<Skill> skills
,@ModelAttribute("vacanciesForCandidate") Set<Vacancy> vacanciesForCandidate
,RedirectAttributes redirectAttributes
)
{
candidateFromRequest.setSkills(skills);
candidateFromRequest.setVacancies(vacanciesForCandidate);//In debug I saw that here exist vacancy
for(Vacancy vacancy:vacanciesForCandidate){
vacancy.getCandidates().add(candidateFromRequest);
}
candidateService.update(candidateFromRequest);//invoke session.update();
redirectAttributes.addAttribute("message", "Submitted Correctly");
redirectAttributes.addAttribute("id",candidateFromRequest.getId());
return "redirect:loadCandidateById";
}
但在此事务之后,数据库中确实存在遗漏的注释。为什么?