-->
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.  [ 9 posts ] 
Author Message
 Post subject: How to refresh a ArrayList from a createQuery ?
PostPosted: Wed Nov 22, 2006 9:30 am 
Regular
Regular

Joined: Mon Jul 31, 2006 4:59 pm
Posts: 53
Hibernate version: 3.2

How can i use the "session.refresh"-method to update my Data-Objects, which i retrieve as a ArrayList ?

Code:
List result;
result = session.createQuery("from Data").list();

-- Refresh please :-)  --


thanks for the answers!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 10:25 am 
Beginner
Beginner

Joined: Mon Nov 13, 2006 8:22 am
Posts: 28
hi,

I don't think refresh() is intended for refreshing lists:

efresh:
Re-read the state of the given instance from the underlying database. It is
inadvisable to use this to implement long-running sessions that span many
business tasks. This method is, however, useful in certain special circumstances.
For example
<ul>
- where a database trigger alters the object state upon insert or update
- after executing direct SQL (eg. a mass update) in the same session
- after inserting a Blob or Clob

regards,
Stijn


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 10:45 am 
Regular
Regular

Joined: Mon Jul 31, 2006 4:59 pm
Posts: 53
So it is recommended to close the session after each query?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 11:05 am 
Beginner
Beginner

Joined: Mon Nov 13, 2006 8:22 am
Posts: 28
maybe I'm don't fully understand what you're trying to do,
if you want to get all the Data-objects from the database you just need to that very thing (createQuery("from Data")). If you want to refresh this list (same session or not) just run that query again.

regards,
Stijn


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 11:12 am 
Regular
Regular

Joined: Mon Jul 31, 2006 4:59 pm
Posts: 53
If i run this query again, the data-objects are not updated when e.g. some data in the database are changed. To update my data-objects with the newest data, i have to refresh or closeSession after i run the query.

Hibernate uses the local copy of my data-objects from the cache.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 11:20 am 
Beginner
Beginner

Joined: Mon Nov 13, 2006 8:22 am
Posts: 28
how are you updating the table, outside the hibernate container?
in that case I guess hibernate doesn't know the data has been updated and will simply return the data from the cache, try to evict the list from the cache before doing a load. If you are updating the data within the container then I guess you're doing it in the same transaction. Try flushing the session before retrieving the list.

regards,
Stijn


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 11:26 am 
Regular
Regular

Joined: Mon Jul 31, 2006 4:59 pm
Posts: 53
Here is a piece of my code. I have a DAO-Class (TerminDAO) where are my methods for reading/saving data from/into the database. Is this allright so?

Code:
class TerminDAO
...
...

public List queryTermin(java.sql.Timestamp from, java.sql.Timestamp to) throws InfrastructureException {
   
        List result;
       
        try {
          
           Session session = HibernateUtil.getSession();
          
             
           String queryString = "from Termin t " 
                                + "where t.timestamp >= :from and "
                             + "t.timestamp <= :to";
           Query q = session.createQuery(queryString)
              .setParameter("from", from, Hibernate.TIMESTAMP)
              .setParameter("to", to, Hibernate.TIMESTAMP);
          
           result = q.list();
                  
           HibernateUtil.commitTransaction();

       
        }
        catch (HibernateException ex) {
            System.out.println("TerminDAO: queryTermin:Error:" + ex.toString());
            throw new InfrastructureException(ex);
          }
         
         
        HibernateUtil.closeSession(); 
       
        return result;
    }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 11:27 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Either call session.clear() to remove all objects from the first level cache, or iterate over the collection and call session.evict(resultElement). Then run the query again to obtain refreshed data.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 04, 2006 4:21 am 
Newbie

Joined: Wed Dec 07, 2005 6:31 am
Posts: 9
I'm facing a similar problem where I really, really want to track which program modules uses which entities. Is there any way to force query.list() to go to the database? Or better, do an onLoad-event like it does for all entites that does not reside in session cache?

_________________
OJ


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