gstackoverflow
gstackoverflow

Reputation: 37034

many to many association. Datas doesn't delete

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.

Upvotes: 2

Views: 150

Answers (1)

V G
V G

Reputation: 19002

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.

Upvotes: 1

Related Questions