OneToMany和ManyToOne:非法尝试将集合与两个打开的会话相关联

时间:2018-01-11 02:57:34

标签: java hibernate

我有2张不同的表"员工"和" Diklat"或者我们可以致电"活动"他们是一个多人和多个人之间的关系。

在SAVE方法的情况下,它的工作正常,但当我尝试继续"删除"来自我的"活动"拥有OneToMany关系的表,它不会工作并给我这种错误,

  

org.hibernate.HibernateException:非法尝试将集合与两个打开的会话相关联

这是我的" 员工" .class表:

@Id
@GeneratedValue
@Column(name = "emp_id")
private Integer emp_id;

@Column(name = "nip")
private String nip;

@Column(name = "nama_pgw")
private String namaPgw;

@Column(name = "pangkat")
private String pangkat;

@Column(name = "golongan")
private String golongan;

@Column(name = "jabatan")
private String jabatan;

@Column(name = "eselon")
private String eselon;

@Column(name = "status")
private String status;

@Column(name = "jam_diklat")
private Long jam_diklat;


@JoinColumn(name = "diklat_id_1", referencedColumnName = "id_diklat")
@ManyToOne(cascade = CascadeType.ALL)
private t_diklat diklat_id_1;

 @JoinColumn(name = "diklat_id_2", referencedColumnName = "id_diklat")
@ManyToOne(cascade = CascadeType.ALL)
private t_diklat diklat_id_2;
@JoinColumn(name = "diklat_id_3", referencedColumnName = "id_diklat")
@ManyToOne(cascade = CascadeType.ALL)
private t_diklat diklat_id_3;
@JoinColumn(name = "diklat_id_4", referencedColumnName = "id_diklat")
@ManyToOne(cascade = CascadeType.ALL)
private t_diklat diklat_id_4;
@JoinColumn(name = "diklat_id_5", referencedColumnName = "id_diklat")
@ManyToOne(cascade = CascadeType.ALL)
private t_diklat diklat_id_5;

这是我的 diklat / Activity .class表:

 @Id
@GeneratedValue
@Column(name = "id_diklat")
private Integer id_diklat;

@Column(name = "nama_diklat")
private String namaDiklat;


@Column(name = "tgl_start")
@Temporal(TemporalType.DATE)
private Date tgl_start;

@Column(name = "tgl_end")
@Temporal(TemporalType.DATE)
private Date tgl_end;

@Column(name = "jam_diklat")
private Long jam_diklat;



@Column(name = "keterangan")
private String keterangan;

@OneToMany(mappedBy = "diklat_id_1")
private Set<t_employee> itemDiklat_1;

@OneToMany(mappedBy = "diklat_id_2")
private Set<t_employee> itemDiklat_2;

@OneToMany(mappedBy = "diklat_id_3")
private Set<t_employee> itemDiklat_3;

@OneToMany(mappedBy = "diklat_id_4")
private Set<t_employee> itemDiklat_4;

@OneToMany(mappedBy = "diklat_id_5")
private Set<t_employee> itemDiklat_5;

这是我的 DaoImplement

  public String delete() {

    diklatT = (t_diklat) (listDiklat.getRowData());
    diklatDao dao = new diklat_Impl();
    dao.remove(diklatT);
    FacesContext context = FacesContext.getCurrentInstance();
    context.addMessage(null, new FacesMessage("Successful", "Data Pegawai " + diklatT.getNamaDiklat() + " Berhasil Diupdate"));
    context.addMessage(null, new FacesMessage("Check Kembali Kelengkapan Data Pegawai"));

    return "diklat_client";
}

这是我的DAO课程

@Override
public void remove(t_diklat diklat) {
    Session session = HibernateUtil.getSessionFactory().openSession();

    try {
        session.beginTransaction();
        session.delete(diklat);
        session.getTransaction().commit();
    } catch (ArithmeticException ex) {
    session.getTransaction().rollback();
        throw ex;
    } finally {
        session.close();
    }

}

1 个答案:

答案 0 :(得分:0)

您在一个Hibernate会话中加载 diklat 对象,并尝试在其他会话中删除。

在Hibernate中,每个实体只属于一个会话,您只能在此会话中使用此实体。