Hibernate Persistent objects and first-level caches

Source: Internet
Author: User

Learn more about how to create hibernate,hibernate manually, and then look at persisted objects and first-level caches.

Hibernate's persistence class has three states:

1, transient transient state: Persistent object does not have a unique identifier OID, not included in the session management.

2, Persistent persistent state: Persistent object has a unique identifier OID, has been included in the management of the session, in addition, the persistence has the characteristics of Automatic Update database

3, detached: Persistent object has a unique identifier OID, not included in the session management.

Here's a piece of code to differentiate between these three states:

1 //differentiate three states of persisted objects:2  Public voiddemo1 () {3     //1. Create Session4Session session =hibernateutils.opensession ();5     //2. Turn on the transaction6Transaction tx =session.begintransaction ();7     8     //to save a book to the database:9Book book =NewBook ();//transient state: There is no uniquely identified OID and is not associated with the session.TenBook.setname ("Hiernate development"); OneBook.setauthor ("Sun xx"); A Book.setprice (65d); -      -Session.save (book);//Persistent State: Has a uniquely identified OID, which is associated with the session. the      -     //3. Transaction Submission - tx.commit (); -     //4. Releasing Resources + session.close (); -      +Book.setname ("Struts2 development");//off-State: There is a unique identifier, not associated with the session. A}

In addition, the conversion of three state objects is as follows:

Transient state: obtained: Book book = new book (), Instantaneous---> Persistent * Save (book), * Save ()/saveorupdate (), instantaneous---> Off tube * book.setid (1); persistence: obtained: Book Book = (book) session.get (book.class,1), * get ()/load ()/find ()/iterate (), Persistent---> instantaneous: * Delete (book); * Special status: Delete state. (Deleted objects are not recommended to use.) Long-Lasting---> removal: * session.close (), * Close ()/clear ()/evict (), off-state: obtained: Book book = new book (), Book.setid (1), off-pipe---> persistent: * Session.update (); * Update ()/saveorupdate ()/lock () off-pipe---> Instantaneous: * Book.setid (NULL);

We mentioned above that the Persistence object has the feature of automatically updating the database, the root cause of which is dependent on the first level cache of Hibernate.

In hibernate, it is divided into two basic caches:

First-level cache and level two cache, in fact, there is a cache, called the query cache, there are books to name it as a level three cache, the following I will introduce first-level cache.

First-level cache: Session-level caching, one-level cache is consistent with the lifetime of the session, is self-bringing, and cannot be uninstalled.

As for the benefits of caching, nothing increases efficiency.

Demonstrate:

1 //proving the existence of a first-level cache2  Public voidDemo3 () {3     //1. Create Session4Session session =hibernateutils.opensession ();5     //2. Turn on the transaction6Transaction tx =session.begintransaction (); 7     8     //the Save method can store data in a first-level cache.9     /*Book Book = new book ();Ten book.setname ("jquery development"); One book.setauthor ("Zhang xx"); A Book.setprice (45d); -      - integer id = (integer) session.save (book); the      - Book Book2 = (book) Session.get (Book.class, id); -      - System.out.println (BOOK2);*/ +      -     //perform two queries with get. +Book Book1 = [book] session.get (book).class, 1);//SQL to query immediately occurs A System.out.println (BOOK1); at      -Book BOOK2 = [book] session.get (book).class, 1);//SQL does not occur because the first-level cached data is used - System.out.println (BOOK2); -      -     //3. Commit a transaction - tx.commit (); in     //4. Close Resources -Session.close ();

When the session loads the customer object, a snapshot is copied for the properties of the value type of the Customer object, which is the copy of the same data in the snapshot area of the first level cache.

When you brush out the cache, you can compare the object's current properties and snapshots to determine which properties of the object have changed, if the data is consistent, not updated, and if the data is inconsistent, update the database automatically.

In hibernate, we can manage the first-level cache in a number of ways:

The first-level cache is related to the life cycle of the session. The session life cycle ends, and the primary cache is destroyed.

Clear (): Empties all the objects in the first-level cache.
Evict (Object obj): Empties an object in the first-level cache.
Flush (): Swipe out of the cache.
Refresh (Object obj): Overwrites the data in the snapshot area with the first level of cached data.

In addition, the timing of a first-level cache

flushmode:* always    : Every time the query is brushed out. Manually call flush. When the transaction commits. * AUTO    : Default. Some queries will be brushed out. Manually calling Flush. * Commit    : When the transaction is committed, manually call flush. * MANUAL    : Only manually calling flush will be brushed out.

Degree of rigor: MANUAL > COMMIT > AUTO > Always

Hibernate Persistent objects and first-level caches

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.