本文介绍了多对多协会。数据不会删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!



  @Entity 
@Table(name =candidate)
@XmlRootElement(name =candidate)
public class Candidate {
@ManyToMany(mappedBy =candidates,fetch = FetchType.EAGER)
@XmlTransient
public Set<空缺> getVacancies(){
返回空缺;
}
....
}



  @Entity 
@Table(name =vacancy)
@XmlRootElement(name =空缺)
公共类空缺{@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name =vacancy_candidate,joinColumns = @JoinColumn(name =vacancy_id),inverseJoinColumns = @JoinColumn name =candidate_id))
public Set< Candidate> getCandidates(){
返回候选人;
}
...
}

控制器方法:

  @RequestMapping(/ saveCandidate)
public String saveCandidate(
Model model,
@ModelAttribute(candidateFromRequest)候选candidateFromRequest,

@ModelAttribute(vacanciesForCandidate)设置<空缺> vacandciesForCandidate,

RedirectAttributes redirectAttributes){

candidateService.updateCandidateAndLinkedEntities(candidateFromRequest,
vacanciesForCandidate,...);
...
}

下一关:



$ p $ @Transactional
@Service(candidateService)
public class CandidateService {
public void updateCandidateAndLinkedEntities(候选人,
设置<空缺>空缺,...){

if(candidate == null)
throw new NullPointerException(null candidate entity); (空缺!=空){

candidate.setVacancies(空位);

(空缺职位空缺){
vacancy.getCandidates()。add(candidate);
vacancyDao.update(空缺);
}
}
candidateDao.update(candidate); //这里是所有权利,我是多么希望(我是关于候选实体的状态)

}


更新方法实现:

空缺:

  @Override 
public boolean update(空缺空缺)抛出HibernateException {
Session session = sessionFactory.getCurrentSession();
if(vacancy == null){
return false;
}
session.update(空缺);
返回true;




$候选人:

  @Override 
public boolean update(Candidate candidate)throws HibernateException {
Session session = sessionFactory.getCurrentSession();
if(candidate == null){
return false;
}
session.update(candidate);

返回true;

}

我有奇怪的行为。如果我给候选人增加新的空缺 - 它的效果很好。但是,如果我删除空缺 - 这是行不通的。



它对我不明白。

当你从候选人名单中删除空缺,不要忘记从空缺名单中删除候选人,反之亦然。还要记住:如果系统中有某个地方发送了已经删除的空缺(反之亦然),那么该实体将再次持续存在。

I have this model:

@Entity
@Table(name = "candidate")
@XmlRootElement(name = "candidate")
public class Candidate{
@ManyToMany(mappedBy = "candidates", fetch = FetchType.EAGER)
    @XmlTransient
    public Set<Vacancy> getVacancies() {
        return vacancies;
    }
....
}

and back mapping:

@Entity
@Table(name = "vacancy")
@XmlRootElement(name="vacancy")
public class Vacancy {@ManyToMany( fetch = FetchType.EAGER)
    @JoinTable(name = "vacancy_candidate", joinColumns = @JoinColumn(name = "vacancy_id"), inverseJoinColumns = @JoinColumn(name = "candidate_id"))
    public Set<Candidate> getCandidates() {
        return candidates;
    }
    ...
}

And I have so Controller method:

@RequestMapping("/saveCandidate")
public String saveCandidate(
        Model model,
        @ModelAttribute("candidateFromRequest") Candidate candidateFromRequest,

        @ModelAttribute("vacanciesForCandidate") Set<Vacancy> vacanciesForCandidate,

        RedirectAttributes redirectAttributes) {

    candidateService.updateCandidateAndLinkedEntities(candidateFromRequest,
            vacanciesForCandidate,...);
    ...
}

next level:

    @Transactional
    @Service("candidateService")
    public class CandidateService {
    public void updateCandidateAndLinkedEntities(Candidate candidate,
                Set<Vacancy> vacancies, ...) {

            if (candidate == null)
                throw new NullPointerException("null candidate entity");


            if (vacancies != null) {

                candidate.setVacancies(vacancies);

                for (Vacancy vacancy : vacancies) {
                    vacancy.getCandidates().add(candidate);
                    vacancyDao.update(vacancy);
                }
            }
            candidateDao.update(candidate);//here all right, how I wish(I am about state of candidate entity)

        }
        ...
    }

update methods realization:

vacancy:

@Override
    public boolean update(Vacancy vacancy) throws HibernateException {
        Session session = sessionFactory.getCurrentSession();
        if (vacancy == null) {
            return false;
        }
        session.update(vacancy);
        return true;

    }

candidate:

@Override
    public boolean update(Candidate candidate) throws HibernateException{
        Session session = sessionFactory.getCurrentSession();
        if (candidate == null) {
            return false;
        }
        session.update(candidate);

        return true;

    }

I have strange behavior. If I add new Vacancy to Candidate - it works good. But if I delete vacancies - it doesn't works.

It is doesn't understand for me.

解决方案

When you remove the Vacancy from the list of the Candidate, don't forget to remove the Candidate from the List of the Vacancy, and vice versa. Also bear in mind: if there is somewhere a place in your system, that send later the already-deleted Vacancy (or vice versa), that entity will be again persisted.

这篇关于多对多协会。数据不会删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 19:10