-->
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.  [ 2 posts ] 
Author Message
 Post subject: Data in DB and hibernate are different
PostPosted: Mon Jan 11, 2010 3:21 am 
Newbie

Joined: Mon Jan 11, 2010 2:56 am
Posts: 2
Hi,

Am new to hibernate. This is my mapping for an Entity called Appointment

Code:
<hibernate-mapping
    package="entity">
    <class name="Appointment" >
        <id name="id" column="id" type="integer">
            <generator class="identity" />
        </id>
        <property name="customerid" />
        <property name="phone" />
        <property name="complain" />
        <property name="appointmentdatetime" />
        <property name="reminder" />
        <property name="ogm" />
        <property name="status" />
       
    </class>
</hibernate-mapping>


I also have the corresponding appointment table in my DB. My application should retrieve all appointments from this table every 30 seconds. The appointment table is updated by an external source. This is the body of my query function

Code:
                Session session = sessionFactory.openSession();
      Query query = session.createQuery("from Appointment as a where a.reminder between :P_start and :P_end");
            
      if(start==null)
         start=Calendar.getInstance();
      
      query.setCalendar("P_start", start);
      
      Calendar end=(Calendar)start.clone();
      end.set(Calendar.HOUR_OF_DAY, 23);
      end.set(Calendar.MINUTE, 59);
      end.set(Calendar.SECOND, 59);   
   
      query.setCalendar("P_end", end);
      
      Debug.log("Quering for appointments between " + start.getTime() + " and " + end.getTime(), true);
      appointments = query.list();
               
      Iterator<Appointment> itr=appointments.iterator();
      
                while(itr.hasNext()){
         Appointment appointment=itr.next();
         scheduler.addJob(appointment);
      }
      
      session.clear();
      session.close();



The problem is, every time the appointment table in DB is updated, the updates are not seen when I re query using my query function. The appointment objects fields are the same as when I did the query for the first time.

Please help


Top
 Profile  
 
 Post subject: Re: Data in DB and hibernate are different
PostPosted: Mon Jan 11, 2010 3:43 am 
Newbie

Joined: Mon Jan 11, 2010 2:56 am
Posts: 2
I figured this out. But again not sure if this the right way to do this. I used session.beginTransaction(); and session.getTransaction().commit();

Code:
Session session = Main.instance.sessionFactory.openSession();

      session.beginTransaction();

      Query query = session.createQuery("from Appointment as a where a.reminder between :P_start and :P_end");
            
      if(start==null)
         start=Calendar.getInstance();
      
      query.setCalendar("P_start", start);
      
      Calendar end=(Calendar)start.clone();
      end.set(Calendar.HOUR_OF_DAY, 23);
      end.set(Calendar.MINUTE, 59);
      end.set(Calendar.SECOND, 59);      
      query.setCalendar("P_end", end);
      
      Debug.log("Quering for appointments between " + start.getTime() + " and " + end.getTime(), true);
      appointments = query.list();
               
      Iterator<Appointment> itr=appointments.iterator();
      while(itr.hasNext()){
         
         Appointment appointment=itr.next();
         System.out.println(appointment.getId() + " " + appointment.getReminder());
         //scheduler.addJob(appointment);
         
      }
      
      session.getTransaction().commit();

      session.close();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.