Bug Hibernate OneToMany / ManyToOne

时间:2015-04-29 17:50:19

标签: java hibernate

我尝试使用OneToMany和ManyToOne建立关系,但是当我尝试将记录插入数据库而不在列表OneToMany中添加对象(类)时,hibernate尝试插入记录OneToMany。例: 员工类:

@Entity
public class Employee implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy="employee")
    private List<Shift> shifts = new ArrayList<Shift>();
}

轮班:

@Entity
public class Shift implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.DETACH)
    @JoinColumn(name="EMPLOYEE_ID")
    private Employee employee;
}

当我尝试插入记录时:

Employee employee = new Employee();
entityManager.persist(employee);
entityManager.flush();

我该如何解决?我正在使用SQL Server数据库。

14:53:49,660 INFO  [stdout] (default task-20) Hibernate: insert into Employee
14:53:49,762 INFO  [stdout] (default task-20) Hibernate: insert into Shift(EMPLOYEE_ID) values (?)

0 个答案:

没有答案