Issue
to-many tables mapped like this :
Resume.java :
@ManyToMany(cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
@JsonIgnore
@JoinTable(
name = "resume_skills",
joinColumns = @JoinColumn(name = "skill_id"),
inverseJoinColumns = @JoinColumn(name = "resume_id"))
private List<Skill> skills = new ArrayList<Skill>();
public void addSkill(Skill skill) {
skills.add(skill);
skill.getResumes().add(this);
}
Skill.java
@ManyToMany(mappedBy="skills")
@JsonIgnore
private List<Resume> resumes;
I do this :
Resume resumeToAdd = new Resume(resume.getGithubAdress(),
resume.getLinkedinAdress(),
resume.getCoverLetter(),
resume.getPicture(),
employee
);
resumeDao.save(resumeToAdd);
Skill skill = skillService.findById(1).getData();
System.out.println(skill);
resumeToAdd.addSkill(skill);
resumeDao.save(resumeToAdd);
But it occurs an error, for example it added a Resume with id 60 and it's trying to add mapping with skill id 60 But it cant because there is no skill in database with id of 60, it should be 1. What did i miss here?
Solution
Looks like you mixed up the order of the mapping. It should be this:
@JoinTable(
name = "resume_skills",
joinColumns = @JoinColumn(name = "resume_id"),
inverseJoinColumns = @JoinColumn(name = "skill_id"))
Answered By - Christian Beikov