簡體   English   中英

休眠:多對多映射例外

[英]Hibernate: Many to many mapping exception

我堅持映射異常。 我在員工角色之間有很多關系。 這是代碼。 角色類

@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

怎么了?

您似乎錯過了角色類的@Id注釋

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM