For the hibernate problem that my colleague met yesterday. Hibernate is the most basic thing. Know, this problem many people have met, also very common, but encountered also often will be ignorant. In order to deepen the impression, know it, know the reason why. After simply using the original Hibernate framework to do some validation, and open the implementation of SQL print output, the conclusion: premise is in the middle of the same transaction: 1, the use of SQL statements, Session.createsqlquery ( SQL). executeupdate (); Insert, the output station prints the SQL INSERT statement, and the SQL statement is used to Session.createsqlquery (SQL). Uniqueresult (); SQL query statements are also printed, and no problems can be queried for the data.  2, using Hibernate encapsulation operation, Session.save (entity); Insert, the output station does not print out the inserted SQL statement, and then use Session.get (Entity,id), method to do the query, nor print out the SQL query statement, but can query the data. To execute a transaction commit statement, the inserted SQL statement is printed out 3, using Hibernate's Session.save (entity); To insert, and then use the "HQL" statement to query, the effect of the 2nd. 4, using Hibernate's Session.save (entity); is inserted, the output table does not print out the inserted SQL statement. The SQL statement is then used for session.createsqlquery (SQL). Uniqueresult (); The SQL query statement is printed. The problem arises and no data is queried. In this case, using the Session.flush () method, the output table prints the inserted SQL statement before the query executes to the flush () method. There is data to query again. After the completion of verification, check the next to the above information, for the 4th, in the development process occurs frequently, very common, I believe many people have encountered, but there are many people continue to touch the brain. Just to deepen the impression. From print console SQL can be seen: A basic hibernate save method of operation Flow: 1. Determine if the instance you want to save is already in the persisted state, and if not, place it in the cache; 2. Plan an INSERT SQL statement based on the instance you want to save, paying attention only to the plan, not the execution; 3. Executed when the transaction is committedThe previously planned INSERT statement; Tx.commit () to Session.flush, at which time the INSERT statement was too printed, but no new records were added to the database, and the main function of the flush method was to clean up the cache. Forces 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 the SQL statements, but not submit to the database. The commit method calls the Flush method first and then commits the transaction. This is why we simply call flush when the record is not inserted into the database, because only the transactions are committed and updates to the database are saved. Because the commit method implicitly calls flush, we generally do not show the call flush method.
This is the flush mechanism of hibernate. In the process of updating and saving some complex objects, it is necessary to consider the change of database operation order and whether the delay flush has effect on the result of the program. If there is a real impact, then it is possible to join flush forcing hibernate to flush the operations recorded in the cache into the database in a place where this sequence of operations needs to be maintained, which may seem less attractive, but effective.
Question: Session.save method, put into the cache, SQL directly query the database is not found out.
After the flush () method, the execution SQL is printed out, but not in the database. SQL Direct query can find out the data. 1. Where is this entity data stored after flush ()? The same cache as the Save () method, the SQL should not be taken. 2, since the Flush method after the data did not enter the database, SQL Direct query, using session.createsqlquery (SQL) query, direct search should not be the database, he queried where?
Hibernate flush Mechanism