Hibernate:多对多映射异常

时间:2017-01-11 18:49:12

标签: java spring hibernate spring-data

我对映射异常感到困惑。我在员工角色之间有很多关系。这是代码。 角色等级

@Entity
@Table(name = "role", catalog = "app")
public class Role implements java.io.Serializable {

@GeneratedValue(strategy = IDENTITY)

@Column(name = "roleId", unique = true, nullable = false)
private Integer roleId;

@Column(name = "title", nullable = false, length = 50)
private String title;

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "roles")
private Set<Employee> employees = new HashSet<Employee>(0);

public Role() {
}

public Role(String title) {
    this.title = title;
}

public Role(String title, Set<Employee> employees) {
    this.title = title;
    this.employees = employees;
}

public Integer getRoleId() {
    return this.roleId;
}

public void setRoleId(Integer roleId) {
    this.roleId = roleId;
}

public String getTitle() {
    return this.title;
}

public void setTitle(String title) {
    this.title = title;
}

public Set<Employee> getEmployees() {
    return this.employees;
}

public void setEmployees(Set<Employee> employees) {
    this.employees = employees;
}

}

员工类

@Entity
@Table(name = "employee", catalog = "app", uniqueConstraints =       @UniqueConstraint(columnNames = "email"))
public class Employee implements java.io.Serializable {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "empId", unique = true, nullable = false)
private Integer empId;

@Column(name = "name", nullable = false, length = 100)
private String name;

@Column(name = "email", unique = true, nullable = false, length = 50)
private String email;

@Column(name = "phone", nullable = false, length = 11)
private String phone;

@Column(name = "ip", nullable = false, length = 20)
private String ip;

@Column(name = "password", nullable = false, length = 200)
private String password;


@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "employee_role", catalog = "app", joinColumns = {
        @JoinColumn(name = "empId", nullable = false, updatable = false) }, inverseJoinColumns = {
                @JoinColumn(name = "roleId", nullable = false, updatable = false) })
private Set<Role> roles = new HashSet<Role>(0);

public Employee() {
}


public Integer getEmpId() {
    return this.empId;
}

public void setEmpId(Integer empId) {
    this.empId = empId;
}

public City getCity() {
    return this.city;
}

public void setCity(City city) {
    this.city = city;
}

public Posts getPosts() {
    return this.posts;
}

public void setPosts(Posts posts) {
    this.posts = posts;
}

public Teams getTeams() {
    return this.teams;
}

public void setTeams(Teams teams) {
    this.teams = teams;
}

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

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

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

public String getPhone() {
    return this.phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getIp() {
    return this.ip;
}

public void setIp(String ip) {
    this.ip = ip;
}

public String getPassword() {
    return this.password;
}

public void setPassword(String password) {
    this.password = password;
}


public Set<Role> getRoles() {
    return this.roles;
}

public void setRoles(Set<Role> roles) {
    this.roles = roles;
}

}

这是例外。还请解释异常

org.hibernate.AnnotationException:mappedBy引用未知的目标实体属性:org.company.app.models.Employee.roles中的org.company.app.models.Role.employees

出了什么问题?

1 个答案:

答案 0 :(得分:0)

看起来你错过了@Id anotation for Role class

car