-->
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: Unable to flush
PostPosted: Fri Nov 09, 2012 6:53 pm 
Newbie

Joined: Fri Nov 09, 2012 6:05 pm
Posts: 1
Hi guys,

I am having a hard time getting hibernate to actually flush changes within my current session. The assertion in the test below fails. Please note that all of the calls to the hibernate session are wrapped in another class (and called through an interface), but I have simplified the code. Tests in the same class are able to retrieve data from the db, but updates don't work.

Code:
      Session session = sessionFactory.getSession();
      session.setFlushMode(FlushMode.ALWAYS);
      
      Transaction transaction = session.beginTransaction();

      Date d = new Date();

      Query q = session.createQuery("update Campaign set shown = :shown where id = :id and shown is null").setInteger("id", id).setDate("shown", d);
      q.executeUpdate();

      Campaign c = (Campaign) session.get(Campaign.class, id);
      

      //This assertion fails
      Assert.assertEquals(c.getShown(), d);


Perhaps, I am misuderstanding how this should work. Shouldn't setting the flush mode to ALWAYS force the hibernate session to flush after the executeUpdate statement?

Anyway, thanks in advance for any help.


Top
 Profile  
 
 Post subject: Re: Unable to flush
PostPosted: Wed Nov 14, 2012 4:35 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi,

the problem is that you are using a bulk-update for updating a single object, that's rather counterproductive.
Bulk-updates are a very special feature and are designed to be used for large updates only.
The changes are then made directly on the database without involve hibernate's persistent context,
that means that changes are not brought back to your persistent context even if you call flush.

The standard procedure would be like following:

Code:
...
Date d = new Date();
Campaign c = (Campaign) session.get(Campaign.class, id);
c.setShown(d); // changes the Shown date in your persistent context (memory).
Assert.assertEquals(c.getShown(), d);
session.flush(); // hibernate now executes the accoding update on database
             // from this moment on, also sucessive queries (where condition) in your transaction will 'see' the updated value
transaction.commit(); // normally a commit implies a implicite flush, so in most configurations the exclicite flush call above is not needed


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.