Hibernate多对一映射将以前的外键设置为NULL

时间:2018-02-24 19:29:42

标签: java hibernate one-to-many many-to-one

我是Hibernate关联映射的新手。当我尝试实现一个小的映射逻辑时,所有以前的外键都会自动更新为Null

档案Employee.java

@Entity
public class Employee extends CommonFields implements Serializable {

    private static final long serialVersionUID = -723583058586873479L;

    @Id
    @Column(name = "id", insertable = false, updatable = false)
    @GeneratedValue
    private Long id;

    private String employeeName;

    @OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
    @JoinColumn(name = "emp_id", referencedColumnName = "id")
    private List<Education> education;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getEmployeeName() {
        return employeeName;
    }

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    public List<Education> getEducation() {
        return education;
    }

    public void setEducation(List<Education> education) {
        this.education = education;
    }

}

档案Education.java

@Entity
public class Education extends CommonFields implements Serializable {

    public Education() {

    }

    private static final long serialVersionUID = -723583058586873479L;

    @Id
    @Column(name = "id", insertable = false, updatable = false)
    @GeneratedValue
    private Long id;

    private String course;

    @ManyToOne
    @JoinColumn(name = "emp_id")
    private Employee employee;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

}

请有人建议解决方案。最近两天我真的坚持了这一部分。

0 个答案:

没有答案