The difference between evict () and clear () in Hibernate

Source: Internet
Author: User

Session.evict (obj): The specified Buffer object will be cleared;

Session.clear (): Clears all objects within the buffer, but does not include objects in the operation.

Hibernate executes in the following order:

(1) A transaction object is generated and the current session is marked as a transaction state (at this point the database-level transaction is not started).

(2) The application uses S.save () to save the object, this time the session will put this object into Entityentries, to mark the object has been associated with the current session, because the application of the object to save the operation, The session also registers the application's insertion behavior in insertions (the behavior includes: object reference, Object ID, Session, persisted Class).

(3) S.evict () detach the object from the S session, and S will remove the object from the Entityentries.

(4) The transaction prompt requires that all caches be flush into the database, session initiates a transaction, and submits all previously enlisted actions in the order of INSERT, update,..., Delete (Note: Update is not performed until all inserts have been executed. Special handling here may also make your program a mess, if you need to control the order of operations, need to use flush), now the object is no longer entityentries, but in the execution of insert behavior only need to access insertions is sufficient, So there will be no exception at this time, the exception appears after inserting the notification session that the object has been inserted complete this step, this step needs to entityentries the object's Existsindatabase flag to True, Since the object does not exist in Entityentres, Hibernate thinks that insertions and entityentries may be out of sync because of thread-safety issues (or whether hibernate developers take into account the processing in the example, If not, this may be a bug, so a net.sf.hibernate.AssertionFailure is thrown and the program terminates.

In general, we will mistakenly think that S.sava will execute immediately, and the object is removed prematurely from the session, resulting in the session insertion and entityentries the content of the different steps. So when we do this, it's important to know when hibernate will flush the data into the database and not detach the manipulated object from the session before flush, the workaround is to add Session.flush after Sava.

The difference between evict () and clear () in Hibernate

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.