Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate: Do I ever need to flush()?

I have a bidirectional one-to-many relationship. I'm trying to persist it like in this doc:

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/example-parentchild.html#example-parentchild-bidir

Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
c.setParent(p);
p.getChildren().add(c);
session.save(c);
session.flush();

Is the flush() required there? What exactly does it do? I know what it does, but I discovered it costs me 100 ms and I would really like to avoid it if possible.

When session.load() or even session.refresh() is called and I hadn't flushed, will it include the new Child in the collection?

like image 428
Konrad Garus Avatar asked Sep 12 '11 12:09

Konrad Garus


People also ask

When should I flush Hibernate?

By default, Hibernate uses the AUTO flush mode which triggers a flush in the following circumstances: prior to committing a Transaction. prior to executing a JPQL/HQL query that overlaps with the queued entity actions. before executing any native SQL query that has no registered synchronization.

Why do we need flush in Hibernate?

Flushing the session forces Hibernate to synchronize the in-memory state of the Session with the database (i.e. to write changes to the database). By default, Hibernate will flush changes automatically for you: before some query executions. when a transaction is committed.

What is flush mode in Hibernate?

The FlushModeType. ALWAYS is Hibernate-specific and tells Hibernate to flush the persistence context before executing a query. Using this mode, Hibernate doesn't check if the flush is required and handles all types of queries in the same way.

What is difference between flush and commit in Hibernate?

You need to flush in batch processing otherwise it may give OutOfMemoryException. Commit(); Commit will make the database commit. When you have a persisted object and you change a value on it, it becomes dirty and hibernate needs to flush these changes to your persistence layer.


1 Answers

The call to flush will synchronise the session state with the database. You do not need to explicitly flush at that point. Hibernate is clever enough to know when it needs to update the database with the session e.g. before executing a query.

That 100ms cost will have to be incurred at some point since it's an inevitable consequence of using a database in your application. Hibernate is able to delay this operation and batch up database changes to minimise round trips to the database.

Hibernate will flush the session when the transaction ends (by default) and so if you call load in the same session you will get the same object back from the session. If you call load in a new session Hibernate will have flushed the changes and your child will be in the collection.

If you were to clear the session or evict your object from the session before a flush was performed you would find that your child is not present.

This chapter from the docs should help.

like image 75
Alex Barnes Avatar answered Oct 05 '22 07:10

Alex Barnes