In depth understanding of flush mechanism in Hibernate

  • 2020-04-01 03:35:32
  • OfStack

The problem with hibernate that my colleague encountered yesterday. Hibernate is the most basic thing. With understanding, this problem has been encountered by many people, is also very common, but encountered often will be confused.

In order to deepen the impression, know how, know why.

Then simply use the original Hibernate framework to do some verification, and open the execution of SQL printout table, the conclusion is:

The premise is that in the middle of the same transaction:

1. Session.createsqlquery (SQL). ExecuteUpdate (); Insert, output table printed SQL insert statement; Session.createsqlquery (SQL).uniqueresult (); Also can print SQL query statement, no problem, can query the data.

2. Use hibernate to encapsulate operations, session.save(entity); Insert, the output table does not print out the inserted SQL statement, and then use session.get(entity,id); Method to do a query; The SQL query is not printed, but the data can be queried. By the time the transaction commit statement is executed, the inserted SQL statement is printed

3. Use hibernate's session.save(entity); Insert, then use "HQL" statement to query, the effect is the same as the second point above.

4. Use hibernate's session.save(entity); Insert, the output console does not print the inserted SQL statement. Session.createsqlquery (SQL).uniqueresult (); SQL queries are printed. There is a problem and no data can be queried. In this case, using the session.flush() method, execute to the flush() method before the query, and the output console prints the inserted SQL statement. If you do a query, you have data.

After the verification, I checked the above data. For the fourth point, it occurs frequently in the development process and is very common. I believe that many people have encountered it, but many people continue to lose their heads. Just to deepen the impression.

From the print console SQL, you can see the operation flow of a basic hibernate save method:

1. Determines whether the instance to be saved is in a persistent state, and if not, places it in the cache.

2. Plan an insert SQL statement based on the instance you want to save. Note that it is only planned, not executed.

3. The previously scheduled insert statement is executed at transaction commit time;

Replace tx.com MIT () with session.flush, where the control too prints the insert statement, but no new records are added to the database;

The primary purpose of the flush method is to clean up the cache and force the database to synchronize with the Hibernate cache to ensure data consistency. Its main action is to send a series of SQL statements to the database and execute them, but not commit them to the database. The commit method first calls the flush method and then commits the transaction. This is why records are not inserted into the database when we simply call flush, because updates to the database are not saved until a transaction is committed. Because the commit method implicitly calls flush, we do not normally display a call to the flush method.

This is hibernate's flush mechanism. In the process of updating and saving some complex objects, it is necessary to consider the change of database operation sequence and whether the delayed flush has an impact on the result of the program. If there is an impact, you can add flush where you need to maintain this order of operations, forcing Hibernate to flush the operations recorded in the cache into the database. This may not look pretty, but it works.

Query: session.save method, put into the cache, SQL direct query database is not checked out.

After the flush() method, the execution SQL is printed, but still not in the database. SQL direct query can find the data.

Where is the entity's data stored after flush()? With the same cache as the save() method, the SQL should be unavailable.

2, since the data does not enter the database after flush method, SQL direct query, using session.createsqlquery (SQL) query, direct search should not be the database, what is his query?


Related articles: