简体   繁体   English

Hibernate / JPA 为什么多对多连接列 ID 为假?

[英]Hibernate / JPA Why many-to-many join column id is false?

to-many tables mapped like this:像这样映射的多表:

Resume.java:简历.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技能.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?但是它发生了一个错误,例如它添加了一个 ID 为 60 的 Resume 并且它正在尝试添加一个技能 ID 为 60 的映射但它不能因为数据库中没有 ID 为 60 的技能,它应该是 1。我在这里错过了什么?

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"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM