0

Hello I'm newbie in learning hibernate framework. I was solved my error but I don't know what the problem happen. In my project I have 2 tables Tblbarang and Tbljenis. And 1 field at Tblbarang had relations as foreign key by Tbljenis.

I want to update Tblbarang table. I had two method

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);
}

This method to set model combobox which I would choose to set the table Tblbarang item.

and now this method to update my Table 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();

when without System.out.print() the error are following

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)

I will simply my code like this

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();

but the exception showing same error. I want to know what happen with my code? anyone can explain with that issue or this is bug from hibernate, thanks

jboxxpradhana
  • 468
  • 2
  • 6
  • 22
  • helo indonesian, same here, give me the full stacktrace . dont use etc..... – user965347 Sep 28 '14 at 15:39
  • and where can u get arrbarang ? just showing full code will make error analyzing better, so show it – user965347 Sep 28 '14 at 15:40
  • okay I was expanded the error stacktrace. and List arrbarang is initialization on method getcombobarang(). I use default mapping and POJOs database to create nbm entity files. – jboxxpradhana Sep 28 '14 at 16:32

1 Answers1

0

In getcombobarang, you have a sess(session1) to get objects from database. And when updating tb, you open another sess(session2).

If Tblbarang contains a foreign-key object, in this case, which must associates with session1, because it's obtained from the function getcombobarang at first. So sess.update() throws an exception as you have seen.

For solution:

  1. use merge() instead of update()
  2. before update, copy the foreign-key object's properties to a whole new object, then set it into tb

I'm also confused about the impact of System.println() here.

Jiang
  • 590
  • 2
  • 10
  • I can do seems update query with merge() method? how can I copy the foreign key, I don't have object properties `Tbljenis` at my frame. yaa before I find the magic `System.println()` I want to trace, I don't know why it happen and my function running as I want. – jboxxpradhana Sep 28 '14 at 16:52
  • @jboxxpradhana do not use the magic and make a comment for this line `tb.setTbljenis(arrbarang.get(combobarang.getSelectedIndex()).getTbljenis());` then try again to see what happens – Jiang Sep 28 '14 at 17:00
  • I was use that. see my seconds format code. but when I remove the System.out.print() I meet those problem. – jboxxpradhana Sep 28 '14 at 17:19
  • @jboxxpradhana That's quite weird. Does `merge()` work? – Jiang Sep 28 '14 at 17:27