冬眠。不会链接到很多很多关联

时间:2013-09-10 14:23:09

标签: java spring hibernate orm

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

在候选人映射中:

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

但是这次交易之后真的有数据库遗漏了注释。为什么?

1 个答案:

答案 0 :(得分:0)

如果您想以这种方式保存,则必须更改映射。现在,Vacancy拥有一方而Candidate拥有。

在您的情况下,您正在使用CascadeType.ALL,因此它应该有效。