为什么System.out.println()可以解决我的Hibernate会话?

时间:2014-09-28 14:51:57

标签: java hibernate hibernate-mapping foreign-key-relationship hibernate-session

您好我是新手学习hibernate框架的。我解决了我的错误,但我不知道问题发生了什么。在我的项目中,我有2个表TblbarangTbljenisTblbarang处的1个字段与Tbljenis的关键字作为外键。

我想更新Tblbarang表。我有两种方法

private void getcombobarang() {
    Query q = sess.createQuery("from Tblbarang");
    arrbarang = new ArrayList<>();

    DefaultComboBoxModel comboModel = new DefaultComboBoxModel();

    for (Object o : q.list()) {
        Tblbarang coba = (Tblbarang) o;
        comboModel.addElement(coba.getNamabarang());
        arrbarang.add(coba);
    }
    combobarang.setModel(comboModel);
}

这个设置模型组合框的方法,我会选择设置表Tblbarang项。

现在这个方法来更新我的表Tblbarang

sess = NewHibernateUtil.getSessionFactory().openSession();
sess.beginTransaction();
Tblbarang tb = new Tblbarang();
tb.setKodbarang(arrbarang.get(combobarang.getSelectedIndex()).getKodbarang());
tb.setNamabarang(arrbarang.get(combobarang.getSelectedIndex()).getNamabarang());
tb.setTbljenis(arrbarang.get(combobarang.getSelectedIndex()).getTbljenis());
tb.setHarganet(arrbarang.get(combobarang.getSelectedIndex()).getHarganet());
tb.setHargajual(arrbarang.get(combobarang.getSelectedIndex()).getHargajual());
System.out.println(arrbarang.get(combobarang.getSelectedIndex()).getTbljenis()); // <-- this line resolved my problem
int st = Integer.parseInt(stok.getText()) ;
int jm = Integer.parseInt(jumlah.getText());
String totss = String.valueOf(st + jm);
Short totstok = Short.parseShort(totss);
tb.setStok(totstok);
sess.update(tb);
sess.getTransaction().commit();

如果没有System.out.print()错误正在跟随

org.hibernate.HibernateException: illegally attempted to associate a proxy with two open Sessions
at org.hibernate.proxy.AbstractLazyInitializer.setSession(AbstractLazyInitializer.java:126)
at org.hibernate.proxy.AbstractLazyInitializer.setSession(AbstractLazyInitializer.java:126)
at org.hibernate.engine.StatefulPersistenceContext.reassociateProxy(StatefulPersistenceContext.java:573)
at org.hibernate.engine.StatefulPersistenceContext.reassociateIfUninitializedProxy(StatefulPersistenceContext.java:533)
at org.hibernate.event.def.ProxyVisitor.processEntity(ProxyVisitor.java:50)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:125)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:83)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:77)
at org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:144)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:314)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:246)
at org.hibernate.event.def.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:57)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireUpdate(SessionImpl.java:742)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.update(SessionImpl.java:722)
at retail.ui.frmBarangMasuk.tambahitemActionPerformed(frmBarangMasuk.java:622) //<-this line directing to sess.update(tb)

我只是像我这样的代码

sess = NewHibernateUtil.getSessionFactory().openSession();
sess.beginTransaction();
Tblbarang tb = (Tblbarang) arrbarang.get(combobarang.getSelectedIndex());
System.out.println(arrbarang.get(combobarang.getSelectedIndex()).getTbljenis());
int st = Integer.parseInt(stok.getText()) ;
int jm = Integer.parseInt(jumlah.getText());
String totss = String.valueOf(st + jm);
Short totstok = Short.parseShort(totss);
tb.setStok(totstok);
sess.update(tb);
sess.getTransaction().commit();

但异常显示相同的错误。我想知道我的代码会发生什么?任何人都可以解释这个问题,或者这是来自hibernate的错误,谢谢

1 个答案:

答案 0 :(得分:0)

getcombobarang中,您有一个sess(session1)来从数据库中获取对象。更新tb时,您打开另一个sess(会话2)。

如果Tblbarang包含一个外键对象,在这种情况下,它必须与 session1 关联,因为它首先从函数getcombobarang获得。所以sess.update()会抛出异常,如你所见。

解决方案:

  1. 使用merge()代替update()
  2. 在更新之前,将外键对象的属性复制到一个全新的对象,然后将其设置为tb
  3. 我也对System.println()在这里的影响感到困惑。