您好,我是JPA新手并且出现以下错误
In attribute 'employees', the "mapped by" value 'project_id' cannot be resolved to an attribute on the target entity.
以下代码
@Entity
@Table(name = "project")
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "project_id")
protected int id;
@Column(name = "project_name")
protected String name;
@ManyToMany(mappedBy = "project_id")
protected List<Employee> employees;
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "employee_id")
protected int id;
@Column(name = "employee_name")
protected String name;
@ManyToMany(targetEntity = Project.class)
@JoinTable(name = "project_employee", joinColumns = @JoinColumn(name = "project_id"), inverseJoinColumns = @JoinColumn(name = "employee_id"))
protected List<Project> projects;
此处如果我试图映射逆实体,则表示该属性未找到。
请帮我解决这个问题
答案 0 :(得分:1)
正如消息所示,您的 mappedBy 值错误。它应该是BIDIRECTIONAL关系另一侧的FIELD / PROPERTY的名称,它链接回此类。例如Project.employees
声称通过字段Employee.project_id
链接回来,但这并不存在。相反, mappedBy 应为projects
。这可以在任何基本的JPA文档中找到