JPA 许多关系“连接列无法解析为类型”错误

2022-09-05 00:03:56

我正在使用Spring boot,并试图实现用户和技能之间的多对多关系。我有一个包含user_id列和skill_id的表users_skills。在尝试实现关系时,我在STS的@JoinColumn注释中不断收到“JoinColumn无法解析为类型”错误。下面是我的用户类

   @Entity
   @Table(name = "users")
   public class User {


@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
private String email;
private String firstName;
private String lastName;
private List<Skill> skills = new ArrayList<Skill>();



protected User() {}

public User(String email,String firstName, String lastName) {
    this.email = email;
    this.firstName = firstName;
    this.lastName = lastName;
}    


public Long getId() {
    return this.id;
}

public void setId(Long id) {
    this.id = id ;
}

public String getEmail() {
    return this.email;
}

public void setEmail(String email) {
    this.email = email ;
}


public String getFirstName() {
    return this.firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName ;
}

public String getLastName() {
    return this.lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName ;
}

@ManyToMany
@JoinTable(name="users_skills", 
            joinColumns={@JoinColumn(name="user_id")},              
          inverseJoinColumns={@JoinColumn(name="skill_id")})    
public List<Skill> getSkills(){
    return skills;
}

public void setSkills(List<Skill> skills) {
    this.skills = skills ;
}

}

答案 1

只需在班级的头上写下这个

import javax.persistence.JoinColumn;

有时 eclipse 不会在上下文菜单中显示导入它的链接,但它就在那里。希望它能帮助某人。


答案 2

推荐