Hibernate Session Cache

Source: Internet
Author: User

First-level caching (view the SQL statement on the console when executing code)

Empty Cache

@Test
public void demo03 () {
Empty cache
Session session=factory.opensession ();
Session.begintransaction ();

1. Enquiry
User user = (user) Session.get (user.class, 1);
SYSTEM.OUT.PRINTLN (user);

SESSION.EVITC (user)//Remove the execution object from the first-level cache
Session.clear (); Empty the first-level cache of all content
Session.close (); The session is closed and cannot be manipulated, otherwise the exception will be thrown. If session is closed, first-level cache is destroyed and data cannot be shared with another session
2. Re-query--The default is to get the content from the first level cache
User User1 = (user) Session.get (user.class, 1);
System.out.println (user1);

Session.gettransaction (). commit ();
Session.close ();
}

Snapshot

Snapshot: A backup of the same data in the first-level cache, which cannot be manipulated directly by the API, and Hibernate maintains itself. Used to align data in a first-level cache with a database

Swipe out cache: Indicates that the first-level cache is compared to the snapshot.

Modify the Flash cache time: Flushmodel to set.

1.get query, Results Store First level cache

2. Modify the content. User.setusername ("Jack");//Data inconsistency

3.1 When query is executed, the UPDATE statement is executed first, and the query is up to date.

3.2 Transaction.commit (): Go to refresh when executing commit

3.3 Session.flush () manually refreshes the cache

API

Save. To cache the transient object in a persistent state object

Primary key generation policy native: When the Save method is executed, insert is triggered, the data is generated OID, and the data is entered into the database after commit

Primary key generation policy assigned: You must manually set the OID and then execute save when the insert is triggered when committed

Update: the de-tube transformation lasts

Update Database with OID

Hibernate Session Cache

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.