0

Help : Hibernate class with many-to-one don't save-update/commit

asked 2009-03-04 20:42:22 +0800

bob007 gravatar image bob007
384 3 4

updated 2009-03-16 14:49:52 +0800

*** 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>


My DAO: HeuresEffectueesDAO.java
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();
   }
}


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

delete flag offensive retag edit

3 Replies

Sort by » oldest newest

answered 2009-03-05 01:03:25 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

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

link publish delete flag offensive edit

answered 2009-03-05 10:53:40 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

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

link publish delete flag offensive edit

answered 2009-03-05 14:00:56 +0800

bob007 gravatar image bob007
384 3 4

updated 2009-03-10 03:33:18 +0800

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

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-03-04 20:42:22 +0800

Seen: 608 times

Last updated: Mar 05 '09

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More