Search code examples
javaspringjpamany-to-many

JPA ManytoMany Relationship "JoinColumn cannot be resolved to a type" error


I am using Spring boot and trying to implement many to many relationship between User and Skill. I have a table users_skills with columns user_id and skill_id. I keep getting "JoinColumn cannot be resolved to a type" error in @JoinColumn annotations in STS when trying to implement the relationship. Below is my User class

   @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 ;
}

}

Solution

  • Just write this at the head of your class

    import javax.persistence.JoinColumn;
    

    Sometime eclipse doesn't show the link to import it in context menu, but it's there. Hope it will help someone.