这里的工作有一组员工,而员工有一组工作,但Spring给我这个例外
引起:org.hibernate.MappingException:外键 (FK1kec5bwba2rl0j8garlarwe3d:account [employee_id]))必须有相同的 列数作为引用的主键(员工 [JOB_ID,EMPLOYEE_ID])
这是我的员工班:
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id ;
private String firstname ;
private String lastname ;
private String email ;
private String phone ;
private String address ;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "account_id")
private Account account ;
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
@ManyToOne
@JoinColumn(name = "departement_id")
@JsonIgnore
private Departement departement ;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "jobs", joinColumns = @JoinColumn(name = "employee_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "job_id", referencedColumnName = "id"))
private Set<Job>jobs ;
....
}
这是我的工作班:
@Entity
@Inheritance
public abstract class Job {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id ;
private String status ; // (pending or done)
private Date date ;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "employee", joinColumns = @JoinColumn(name = "job_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "employee_id", referencedColumnName = "id"))
private Set<Employee> employee ;
@ManyToOne
@JoinColumn(name = "BusinnesPartner_id")
@JsonIgnore
private BusineesPartner busineesPartner ;
}
请你解释一下为什么我会得到这个例外。
答案 0 :(得分:0)
@JoinTable注释应该在拥有实体端使用,在另一方面你不应该有@JoinTable,你需要有mappedBy来定义反向关系,因为你建立双向关系是如果Job是拥有您需要修改Employee pojo的实体
@ManyToMany(cascade = CascadeType.ALL, mappedBy="employee")
private Set<Job>jobs ;