-
FEATURED COMPONENTS
First time here? Check out the FAQ!
*** ANSWERED/SOLVED ***
Hi all,
I did like in Small Talks - Hibernate + ZK exemple. And it worked well until i add a many-to-one relation in my hibernate mapping. Now i can load my objects with the referenced objects and access it. BUT it seem I can't save a new object (with many-to-one relation) without create a transaction and i don't think it's a good thing to do in zk.
DONT WORK:
If I do like in the smalltalk exemple: I don’t create a transaction
i can see the insert into (…) line in the console BUT its never added to the database.
WORK:
If i comment the mapping line <many-to-one name="repondant" column="cipRepondant" class="repondants.Repondant" lazy="false" insert="false" update="false"/> i can save/update with no problem.
WORK:
If i create i create a transaction it commit well WITH my many-to-one mapping.
WORK:
I don’t create a transaction, I use many-to-one mapping BUT i never access the referenced object ((//((HeuresEffectuees)data).getRepondant().getNom())) . I can save-update can save/update with no problem.
Why this behavior?
Should I create a transaction then ? When ? Each time ?
My mapping : HeuresEffectuees.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="heuresEffectuees.HeuresEffectuees" table="heuresEffectuees"> <composite-id> <key-property name="noBon"/> <key-property name="cipRepondant"/> </composite-id> <property name="nbHeuresEffectuees"/> <many-to-one name="repondant" column="cipRepondant" class="repondants.Repondant" lazy="false" insert="false" update="false"/> </class> </hibernate-mapping>
public class HeuresEffectueesDAO { Session currentSession() { return HibernateUtil.currentSession(); } //!!!!!!!! DON’T WORK !!!!!!!! public void saveOrUpdate(HeuresEffectuees desHeuresEffectuees) { Session sess = currentSession(); sess.saveOrUpdate(desHeuresEffectuees); } //!!!!!!!!!!!! WORK !!!!!!!!!!! public void saveOrUpdate2(HeuresEffectuees desHeuresEffectuees) { Session sess = currentSession(); Transaction tx = sess.beginTransaction(); sess.saveOrUpdate(desHeuresEffectuees); tx.commit(); } public void delete(HeuresEffectuees desHeuresEffectuees) { Session sess = currentSession(); sess.delete(desHeuresEffectuees); } public List findAllByNoBon(String noBon) { Session sess = currentSession(); Criteria criteria = sess.createCriteria(HeuresEffectuees.class); criteria.add(Restrictions.like("noBon", noBon)); return criteria.list(); } public HeuresEffectuees findByNoBonEtRepondant(String noBon, String cipRepondant) { Session sess = currentSession(); return (HeuresEffectuees) sess.get(HeuresEffectuees.class, cipRepondant); } public List findAll() { Session sess = currentSession(); return sess.createQuery("from HeuresEffectuees").list(); } }
public class HeuresEffectueesRowRenderer implements RowRenderer { public void render ( Row row, Object data ){ //if i access repondant then i can't save-update a new HeuresEffectuees. new Label(((HeuresEffectuees)data).getRepondant().getNom()).setParent(row); new Label(((HeuresEffectuees)data).getNbHeuresEffectuees().toString()).setParent(row); new Label("").setParent(row); } }
Did you DAO work fine in Traditional Web Application ?
(I think this is hibernate problem, maybe you should ask this kind of question at hibernate forum)
If your DAO work in Traditional Web Application, please go back here, then we discuss more detail about it.
/Dennis
You must all time use a transaction like tx.commit.
What's when the insert in first table works and in the second table fails?
The transaction is needed for the database consistence.
regards
Stephan
Finally it was totally NOT A PROBLEM RELATED TO ZK.
It was only a problem of detached object not attached to the new referring object.
Since I don’t insert/update the referred object I didn’t think I must attach it anyway.
Thanks for your help.
Steve
Asked: 2009-03-04 20:42:22 +0800
Seen: 608 times
Last updated: Mar 05 '09