-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Updating several objects in the same Session
PostPosted: Mon Mar 08, 2004 7:27 am 
Newbie

Joined: Tue Sep 16, 2003 6:12 am
Posts: 13
This should be very simple, but i've tried it in several different ways and still can't get it right....

What i need is to get an object's collection and then depending on a condition such as
Code:
if(currentTime >= lap){
updating an object's field.

Seems that there is a problem with the persistance or/and the Session, because every time there's more than one Escalation object in the it Iterator, it only reaches the first object and then stops.....No error messages, no Exceptions thrown, just nothing!!!???

Here's the code:
Code:
try{   

               session = sf.openSession();
               Query query = session.createQuery("from attend.entity.Escalation esc where esc.iniTime <= " + ini + " and esc.endTime >= " + ini + " and esc.on = 'y' and esc.server = '" + paramValue + "' order by esc.order asc");
               Iterator it = query.iterate();

               while(it.hasNext()){
            
                   Escalation esc = (Escalation)it.next();   
            
                   long currentTime = date.getTime();
                   long lap =  esc.getLastExe() + (esc.getLap() * 60 * 1000);
                       
                   if(currentTime >= lap){
               
                         try{
                                                       
                               esc.setLastExe(date.getTime());
                               session.saveOrUpdate(esc);      
                               session.flush();   
                               session.connection().commit();                     
                         }
                         catch (SQLException sqle) {

                              System.out.println("SQLException in class attend.Escale: errors in actualizing lastExe field ->" + sqle.getMessage());
                         }
                         catch (HibernateException he) {
   
                              System.out.println("HibernateException in class attend.Escale: errors in actualizing lastExe field ->" + he.getMessage());
                         }
                   }
                }
         
                session.close();
}
catch (HibernateException he) {
   
                System.out.println("HibernateException in class attend.Escale: " + he.getMessage());
}


and here's is the schema:
Code:
<class name="attend.entity.Escalation" table="sys_escalation">
      <id name="code" column="esc_esccod" type="string">
         <generator class="assigned" />
      </id>
      
      <property name="obj" column="esc_escobj" type="string" />
      <property name="from" column="esc_escfrm" type="string" />
      <property name="where" column="esc_escwhr" type="string" />
      <property name="server" column="esc_escsrv" type="string" />
      <property name="order" column="esc_escord" type="int" />
      <property name="iniTime" column="esc_initme" type="int" />
      <property name="endTime" column="esc_endtme" type="int" />
      <property name="lap" column="esc_esclap" type="int" />
      <property name="lastExe" column="esc_lstexe" type="long" />
      <property name="on" column="esc_escaon" type="string" />
      <property name="observation" column="esc_escobs" type="string" />
      <property name="active" column="esc_active" type="string" />
   </class>



I guess there's something wrong with the use/order of the Session's methods. Please help
:-)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 08, 2004 8:58 am 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
Hi, I don't see anything wrong... of course, it could be that only 1 item matches your where clause? Are you certain that more than 1 object should be returned? I would grab the sql generated from hibernate, and run directly against the db to make sure your query is okay.

In addition- just fyi, you don't need to explicitly call save on the obhect:

esc.setLastExe(date.getTime());
session.saveOrUpdate(esc);
session.flush();
session.connection().commit();

inside the loop. Hibernate will notice the things that get updated without calling saveOrUPdate(esc)

If you call commit(), flush is automatically called AFAIK.

If you want commit/rollback- I always use
tx = session.beginTransaction()
//do stuff

tx.commit

You have session.connection().commit()... not sure why you would do that


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 08, 2004 9:00 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Why do you flush and commit everytime

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 08, 2004 10:20 am 
Newbie

Joined: Tue Sep 16, 2003 6:12 am
Posts: 13
.....
Quote:
Why do you flush and commit everytime

I'm not sure why do i need to flush and commit every time. I thought that only flushing should be alright, but it didn't synchro the data

Quote:
In addition- just fyi, you don't need to explicitly call save on the obhect:

What do you mean, something like this?

Code:
esc.setLastExe(date.getTime());
session.flush();
session.connection().commit();


Thanks!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.