我有两个实体类。
@Entity
公共类Employee {
@Id
@Column(name = "employee_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "employee_skill", joinColumns = @JoinColumn(referencedColumnName = "employee_id"), inverseJoinColumns = @JoinColumn(referencedColumnName = "skill_id"))
private Collection<Skill> listSkill;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Collection<Skill> getListSkill() {
return listSkill;
}
public void setListSkill(Skill skill) {
if (listSkill == null) {
listSkill = new ArrayList<Skill>();
}
listSkill.add(skill);
}
}
@Entity
公共类Skill {
@Id
@Column(name = "skill_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
@ManyToOne(cascade = CascadeType.ALL)
@JoinTable(name = "employee_skill", joinColumns = @JoinColumn(referencedColumnName = "skill_id", insertable = false, updatable = false), inverseJoinColumns = @JoinColumn(referencedColumnName = "employee_id", insertable = false, updatable = false))
private Employee employee;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}
当两者都是UNI-Directional时,我可以保存与技能关联的员工对象。
现在,当我试图使它双向,我得到sql错误说,Field 'skill_id' doesn't have a default value
我不知道我哪里做错了。
这里没有双向的onetmany关联。有两个独立的关联映射到同一个连接表。由于mappedBy
属性
@OneToMany(cascade = CascadeType.ALL, mappedBy = "employee")
private Collection<Skill> listSkill;