3

我有候选和空缺实体(多对多注释)。

在候选映射中:

@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";
    } 

但在此事务之后,数据库中确实存在遗漏的注释。为什么?

4

1 回答 1

0

如果要以这种方式保存,则必须更改映射。现在,Vacancy是拥有方和Candidate拥有。

在您的情况下,您正在使用CascadeType.ALL它应该可以工作。

于 2013-09-11T13:14:38.950 回答