geoandri
geoandri

Reputation: 2428

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

}

Upvotes: 11

Views: 19870

Answers (2)

Avijit Nagare
Avijit Nagare

Reputation: 8782

I ended up with importing below line:

import jakarta.persistence.JoinColumn;

For, below relationship.

@ManyToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)   
    @JoinTable(name = "user_role", joinColumns = {
            @JoinColumn(name = "user", referencedColumnName = "id")},
            inverseJoinColumns = { @JoinColumn(name = "role", referencedColumnName = "id")})
        
private Set<Role> roles = new HashSet<>();

I was using MySql workbench. Hope, this work for someone.

Upvotes: 0

Cedriga
Cedriga

Reputation: 4156

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.

Upvotes: 46

Related Questions