Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate: Refresh, Evict, Replicate and Flush

Tags:

java

hibernate

I wish I knew what exactly does each item in this list, how it works, what the consequences and when is the correct time to use.

  1. Refresh
  2. Evict
  3. Replicate
  4. Flush

I even wonder what each one does, but I'm not absolutely sure, so I'm asking for your help, cause I really want to understand it.

I know it's a pretty generic question, but I think really useful to know about it all.

Thanks.

like image 599
caarlos0 Avatar asked Nov 15 '11 13:11

caarlos0


People also ask

What is refresh method in hibernate?

In this hibernate tutorial, we will discuss the basics of and differences between refresh() and merge() methods present in hibernate Session interface. At a very high level, refresh() means pulling any state changes from the database that have been done outside the current Session and after the entity has been loaded.

What does hibernate flush do?

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.

When should I flush my hibernate session?

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.

What is flush and clear in hibernate?

Manual, the programmer is informing hibernate that he/she will handle when to pass the data to the database. Under this configuration the session. flush() call will save the object instances to the database. A session. clear() call acutally can be used to clear the persistance context.


1 Answers

The Hibernate Documentation gives good examples of this. Also this blog post will give you some insight. I will add some line from there below.

It is possible to re-load an object and all its collections at any time, using the refresh() method. This is useful when database triggers are used to initialize some of the properties of the object.

sess.save(cat); sess.flush(); //force the SQL INSERT sess.refresh(cat); //re-read the state (after the trigger executes) 

see here for more examples.

Whenever you pass an object to save(), update() or saveOrUpdate(), and whenever you retrieve an object using load(), get(), list(), iterate() or scroll(), that object is added to the internal cache of the Session.

When flush() is subsequently called, the state of that object will be synchronized with the database. If you do not want this synchronization to occur, or if you are processing a huge number of objects and need to manage memory efficiently, the evict() method can be used to remove the object and its collections from the first-level cache.

ScrollableResult cats = sess.createQuery("from Cat as cat").scroll(); //a huge result set while ( cats.next() ) {     Cat cat = (Cat) cats.get(0);     doSomethingWithACat(cat);     sess.evict(cat);     //  (if gives the compile time error then use it: sess.evict(cat.getClass());   } 

Read the complete example from here.

Read about the session API here.

like image 73
ManuPK Avatar answered Oct 01 '22 06:10

ManuPK