File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate: When to flush Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Hibernate: When to flush" Watch "Hibernate: When to flush" New topic
Author

Hibernate: When to flush

Choon-Chern Lim
Ranch Hand

Joined: Aug 29, 2005
Posts: 74
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.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 16622

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Choon-Chern Lim
Ranch Hand

Joined: Aug 29, 2005
Posts: 74
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?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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 ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Choon-Chern Lim
Ranch Hand

Joined: Aug 29, 2005
Posts: 74
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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Aug 29, 2005
Posts: 74
Thanks for the explanation, Paul.... it makes more sense to me now.
 
 
subject: Hibernate: When to flush
 
Threads others viewed
No SQL result unless i reopen
Batch Update in Hibernate
run query in the same time
CMT & Transaction Attribute
JDO issue
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture