-->
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.  [ 3 posts ] 
Author Message
 Post subject: No auto flush after save before select -why???
PostPosted: Sun Oct 18, 2009 4:33 am 
Newbie

Joined: Sun Oct 18, 2009 3:44 am
Posts: 2
Hi all,

http://docs.jboss.org/hibernate/core/3. ... e-flushing states that the session is flushed before query executions. This is not happening.

Code:
// no transaction
session.setFlushMode(FlushMode.AUTO); // no change with ALWAYS

session.save(new Book("THE ASSOCIATE")); // book has title only, set by constructor
session.save(new Book("CROSS COUNTRY"));
session.save(new Book("COVET"));
session.save(new Book("HEAT LIGHTNING"));

System.out.println(session.createQuery("select count(*) from Book").uniqueResult()); // prints 0
System.out.println(session.get(Book.class, 1l)); // prints book instance
System.out.println(session.createQuery("select count(*) from Book").uniqueResult()); // prints 0

session.flush();
System.out.println(session.createQuery("select count(*) from Book").uniqueResult()); // prints 4


This behaviour is observed for the 'increment' and 'native' (sequences, if supported) id generator with both mysql and postgresql. Shouldnt hibernate flush automatically before executing the query??

This causes a lot of pain. Why doesn't hibernate persist before executing the query??

Any hints are appreciated,
Juri

References:
http://jira.codehaus.org/browse/GRAILS-2662
https://forum.hibernate.org/viewtopic.php?f=1&t=994006&p=2404056&hilit=+save+flush#p2404056
https://forum.hibernate.org/viewtopic.php?f=1&t=988889&p=2390919&hilit=+save+flush#p2390919
https://forum.hibernate.org/viewtopic.php?f=1&t=981782&p=2370621&hilit=+save+flush#p2370621
https://forum.hibernate.org/viewtopic.php?f=1&t=981037&p=2368630&hilit=+save+flush#p2368630


Top
 Profile  
 
 Post subject: Re: No auto flush after save before select -why???
PostPosted: Thu Oct 29, 2009 4:04 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
From SessionImpl based on your comment where you said you weren't inside a transaction

Code:
   /**
    * detect in-memory changes, determine if the changes are to tables
    * named in the query and, if so, complete execution the flush
    */
   protected boolean autoFlushIfRequired(Set querySpaces) throws HibernateException {
      errorIfClosed();
      if ( ! isTransactionInProgress() ) {
         // do not auto-flush while outside a transaction
         return false;
      }
      AutoFlushEvent event = new AutoFlushEvent(querySpaces, this);
      AutoFlushEventListener[] autoFlushEventListener = listeners.getAutoFlushEventListeners();
      for ( int i = 0; i < autoFlushEventListener.length; i++ ) {
         autoFlushEventListener[i].onAutoFlush(event);
      }
      return event.isFlushRequired();
   }



_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
 Post subject: Re: No auto flush after save before select -why???
PostPosted: Thu Oct 29, 2009 7:26 pm 
Newbie

Joined: Sun Oct 18, 2009 3:44 am
Posts: 2
That's it! Within a transaction the session is flushed as expected.

Thanks a lot!
Greetings,
Juri


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