-->
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.  [ 6 posts ] 
Author Message
 Post subject: Force hibernate to get objects from DB
PostPosted: Wed Jan 23, 2008 10:18 am 
Newbie

Joined: Fri Oct 05, 2007 7:35 am
Posts: 8
Hello everybody,

My question his how do i force hibernate to get an object from the db.
I use the following code:
Code:
return (AwsAccount) this.getHibernateTemplate().load(AwsAccount.class, id);

To get an object in a HibernateDaoSupport class
Only it gets an old version from the cache. Because I don't see any query in the console.

So can anybody tell me how do i force hibernate to get an object from the db.

thank you in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 23, 2008 11:14 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Your second-level cache might be enabled. In that case, your objet is being retrieved from memory or some disk directory, in order to save trips to the database. That is not necessarily a bad thing.

If you are still in doubt, make a query on that ID. The query.list() method is guaranteed to return always fresh data.

But asking "how to get always the latest from the database" is perhaps the wrong question. Hibernate should not coexist with other mechanisms that also modify the database. If other things are changing the database, you should change that. If no other things are changing the database, then don't worry and let Hibernate handle how to the latest.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 23, 2008 11:34 am 
Newbie

Joined: Fri Oct 05, 2007 7:35 am
Posts: 8
I made the following code:
Code:
System.out.println("get");
      System.out.println(this.getSession().createSQLQuery("SELECT awsaccount_instances FROM awsaccount WHERE awsaccount_id = " + id).list().get(0));
      AwsAccount awsAccount = (AwsAccount) this.getHibernateTemplate().get(AwsAccount.class, id);
      System.out.println("aws-" + awsAccount.getInstances());
      System

And it give different results.
So how do I force the DB to give me the result of the db?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 23, 2008 12:54 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
I would assume it is a problem of multiple sessions. The HibernatTemplate's session, plus that other session you call.
They don't know about each other, and each one is caching its own modifications to the database.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 24, 2008 4:11 am 
Newbie

Joined: Fri Oct 05, 2007 7:35 am
Posts: 8
I know that's the problem. But i want to know what the solution is.
Only solution what i know is to use createSQLQuery.
But there must be another solution.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 28, 2008 4:56 am 
Newbie

Joined: Fri Oct 05, 2007 7:35 am
Posts: 8
Does somebody know a solution for my problem?


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