Deep understanding of flush mechanism in hibernate _java

Source: Internet
Author: User

In response to the hibernate problem that my colleague met yesterday. Is hibernate the most basic thing. With understanding, this problem many people have encountered, but also very common, but encountered also often Meng.

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

After the simple use of the original Hibernate framework to do some validation, and open the execution of SQL printing output, concluded:

The premise is in the middle of the same transaction:

1, the use of SQL statements, Session.createsqlquery (SQL). executeupdate (); Insert, the output table prints out the SQL INSERT statement, and then use the SQL statement for session.createsqlquery (SQL ). Uniqueresult (); The SQL query statement is also printed and there is no problem and you can query the data.

2, the use of Hibernate encapsulation operation, Session.save (entity); Insert, the output table does not print out the inserted SQL statements, and then use Session.get (Entity,id) method to do the query, and did not print out the SQL query statement, but can query the data. When the transaction commit statement is executed, the inserted SQL statement is printed

3, the use of Hibernate session.save (entity); To insert, and then use the "HQL" statement to query, the effect of the 2nd above.

4, the use of Hibernate session.save (entity); To insert, the output console does not print out the inserted SQL statement. The SQL statement is used to Session.createsqlquery (SQL). Uniqueresult (); The SQL query statement is printed. The problem occurs and the query does not have any data. In this case, the Session.flush () method is used to execute to the flush () method before the query, and the output table prints out the inserted SQL statement. Then the query will have data.

Verification completed, looked up the data, for the 4th, in the development process occurs frequently, very common, I believe many people have encountered, but many people continue to touch the mind. Just to deepen the impression.

From print console SQL you can see a basic hibernate Save method operation process:

1. Determines whether the instance being saved is in a persisted state, and if not, put it into the cache;

2. Schedule an Insert SQL statement based on the instance you want to save, note that it is only planned and not executed;

3. Execute the INSERT statement that was scheduled before the transaction is committed;

Replace the Tx.commit () with the Session.flush, at which point the control too prints the INSERT statement, but no new records are added to the database;

The main function of the flush method is to clean 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 the SQL statements, but not submit to the database. The Commit method first invokes the Flush method, and then commits the transaction. This is why the record is not inserted into the database when we simply invoke flush, because the update to the database will not be saved until the transaction is committed. Because the commit method implicitly invokes the flush, we generally do not display the call to the Flush method.

This is the flush mechanism of hibernate. In the process of updating and saving of some complex objects, it is necessary to consider the change of database operation order and whether the delay flush has influence on the result of the program. If you do have an impact, you can add flush to force hibernate to flush the operations recorded in the cache to the database where you need to keep the order of operations, which may not look good, but it works.

Question: Session.save method, put into the cache, SQL direct query database is not found.

After the flush () method, the execution SQL is printed out, but not in the database. SQL Direct query can detect data.

1. Where is this entity data stored after flush ()? The same cache as the Save () method, the SQL should not be.

2, since the Flush method data does not go into the database, SQL Direct query, using session.createsqlquery (SQL) query, directly check should not be a database, he inquires what place?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.