• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate: When to flush

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it recommended to flush everytime when insert, update or delete operation is performed?

In my web application, I currently use one transaction per user request, where all insert/update/delete/query are done before comitting the transaction and redirect to jsp. However, in one of my unique scenarios, I noticed the newly inserted row doesn't have an ID value assigned to it yet (assigned as "native" since my primary key uses DB identity) after I queried all rows.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where have you read that it is recommended to flush after every CRUD operation? That will cause more network and database traffic, which slows performance a little bit.

Mark
 
Choon-Chern Lim
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If user A inserts a new record without flushing it, and user B queries the record in split second, wouldn't user B acquire that record with null ID assigned to it?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Choon-Chern Lim:
If user A inserts a new record without flushing it, and user B queries the record in split second, wouldn't user B acquire that record with null ID assigned to it?



How would user B manage to query for a record that doesn't yet exist?


However, in one of my unique scenarios, I noticed the newly inserted row doesn't have an ID value assigned to it yet (assigned as "native" since my primary key uses DB identity) after I queried all rows.


You have an entity defined that allows a null primary key? How did your RDBMS allow you to get away with that? I ask since this violates one of the rules of that defines a PK, namely that it is unique, unchanging and not null.
[ August 09, 2006: Message edited by: Paul Sturrock ]
 
Choon-Chern Lim
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All my primary keys are defined something like this:-



In my current application, user A inserts a new record and acquires the newly inserted record.



In the above scenario, the printout displays null ID value.

When I look at the log, it seems like Hibernate only writes to database right after I do the printout.



When I do a flush prior to printout, then it works as expected:-



In this example, is it the time I would need to flush the session? Or is there other way to obtain the ID value?

The one-to-many relation between these 2 tables are set up like this:-



Any suggestion is appreciated. Thanks.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the downsides of allowing the database to generate your id is you will have to commit the transaction in order to allow it to do this. So in the context of your application, yes you will have to flush after every insert. You won't have to flush after every update though, since an update will never change the PK (if it does, its really an insert). Nor should you need to flush after a delete, since the PK is meaninless after a delete.
 
Choon-Chern Lim
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation, Paul.... it makes more sense to me now.
reply
    Bookmark Topic Watch Topic
  • New Topic