-->
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.  [ 5 posts ] 
Author Message
 Post subject: Query.list() doesn't see newly-inserted data
PostPosted: Tue Jul 25, 2006 5:02 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 6:15 am
Posts: 22
Hibernate version:
3.1.3

Code between sessionFactory.openSession() and session.close():
Asset asset = new Asset();
asset.setAttribute("abc");
session.save(asset);

String hql = "from Asset as asset where asset.attribute=?";
List assets = session.createQuery(hql).setParameters(new String[] { "abc" }, new Type[] { new StringType() }).list();
Iterator it = assets.iterator();
if (it.hasNext()) {
Asset a = (Asset)it.next();
System.out.println(a.getAttribute());
} else {
throw new IllegalStateException("no data");
}

Name and version of the database you are using:
Oracle 10.2.0.2

In Hibernate 2.1.8, I use session.find() rather than query.list() but the otherwise identical code worked (it printed the attribute). After having followed the migration guide and upgraded to 3.1.3, I no longer get the result.

I turned on Hibernate logging and found that the INSERT statement occurs after the IllegalStateException has been thrown to the caller. It appears that Hibernate didn't call flush before list(). I tried session.setFlushMode(FlushMode.ALWAYS) but it didn't help; an explicit session.flush() before query.list() did.

Did I do something wrong? I don't want to call flush() after every save(). Worse, what about update (by calling setters)? Is there something obvious I missed?

Thanks
Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 25, 2006 5:43 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 6:15 am
Posts: 22
By the way, session.load() can see the newly-inserted object. But query.list() still can't. Casting session to org.hibernate.classic.Session and using find() also doesn't work.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 25, 2006 6:08 am 
Beginner
Beginner

Joined: Tue May 23, 2006 4:10 pm
Posts: 38
Location: Charleston, SC
Are you not showing us the transaction in your code snippet or do you use CMT? The behavior you observed is normal. Hibernate waits as long as possible to execute the SQL insert statement unless you explicitly commit a transaction or execute a method like flush() on the session.

Try this code:

Code:
Asset asset = new Asset();
asset.setAttribute("abc");
Transaction tx = session.beginTransaction()
session.save(asset);
tx.commit();

String hql = "from Asset as asset where asset.attribute=?";
List assets = session.createQuery(hql).setParameters(new String[] { "abc" }, new Type[] { new StringType() }).list();
Iterator it = assets.iterator();
if (it.hasNext()) {
Asset a = (Asset)it.next();
System.out.println(a.getAttribute());
} else {
throw new IllegalStateException("no data");
}


Grant


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 25, 2006 10:18 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 6:15 am
Posts: 22
Hi Grant

Thanks for your reply. I understand how Hibernate delays pending updates to as late as possible - we depend critically on this feature for performance, as well as batching multiple updates into one if they are about the same table.

The documentation expressly said Query.list() will not return stale data nor will it return wrong data (10.10. Flushing the Session). I didn't show transaction code because all the code I showed belong to one transaction. I expect, in the same transaction, if I insert something I should be able to query it subsequently. This is consistent with SQL. Otherwise, it'd be stale or wrong data, wouldn't it? Anyway, the very same code works in Hibernate 2.1.8.

If I have to manually flush before every find it'd be a performance nightmare...

Kevin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 25, 2006 4:20 pm 
Beginner
Beginner

Joined: Tue May 23, 2006 4:10 pm
Posts: 38
Location: Charleston, SC
Kevin, I can't comment further on the behavior you're observing without writing a test. I agree that a query done in the same transaction should return the updates and the Hibernate docs do state Query.list should never return stale or wrong data. My only other suggestion would be to try the latest 3.x release. It might be a bug in 3.1.

Grant


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