Evict, clear, and flush methods for hibernate caches

Source: Internet
Author: User

The evict (), clear (), and Flush () methods are the 3 basic operational methods of hibernate caching, and this article mainly describes how to use the 3 methods and the specific differences.

Company table:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7C/A6/wKioL1bVHaPArE8EAAAN7mNGBzQ433.png "title=" 1.PNG " alt= "Wkiol1bvhapare8eaaan7mngbzq433.png" style= "float:none;"/>

Company Entity class:

import java.util.set;public class company {    private int  companyid;    private string companyname;    private  Set<employee> companyemployees;    public int getcompanyid ()  {         return companyId;    }     public void setcompanyid (Int companyid)  {         this.companyId = companyId;    }    public  String getcompanyname ()  {        return companyName;     }    public void setcompanyname (String companyName)  {        this.companyName = companyName;     }     public set<employee> getcompanyemployees ()  {         return companyEmployees;    }    public  Void setcompanyemployees (set<employee> companyemployees)  {         this.companyemployees = companyemployees;    }}

company HBM configuration:

1. Evict () and clear () methods
Both the evict () and the Clear () methods clear the cache from the session, evict () clears the cache for a single object, and clear () clears all caches. The test method is as follows:

Company Company = (company) session.get (Company.class, 1); ASystem.out.println (Company.getcompanyname ());  Bcompany = (company) session.get (Company.class, 1); CSystem.out.println (Company.getcompanyname ()); D

A: Send SQL to database query here.
B: Print out here: KONAMI.
C: The cache is taken here and no SQL is sent.
D: Print out here: KONAMI.
Below we clear the cache before the second get:

Company Company = (company) session.get (Company.class, 1); ASystem.out.println (Company.getcompanyname ()); Session.evict (company);  Bcompany = (company) session.get (Company.class, 1); CSystem.out.println (Company.getcompanyname ());

A, C: These two places will send the same SQL to the database query. Because the cache is cleared at B, the same effect is called the Clear () method.



2. Flush () method

The flush () method generates an appropriate SQL statement to manipulate the database based on the operation of the object in the cache. The test method that does not call flush () is as follows:

Company Company = (company) session.get (Company.class, 1); Company.setcompanyname ("Santa Monica"); Session.update ( Company); Acompany Newcompany = new Company (), Newcompany.setcompanyname ("UBISOFT"); Session.save (Newcompany); Btransaction.commit (); Csession.close ();

A: SQL is not issued here to update the database.

B: The INSERT statement is issued here to update the database, but the data is uncommitted and can be seen if the isolation level of the database is read committed.

INSERT INTO company (company_name) VALUES (?)

C: An UPDATE statement is issued here to update the company information.

Update company set company_name=? where company_id=?

The SQL statement is issued here because Transaction.commit () calls the Flush () method.

Let's call the Flush () method ourselves:

Company Company = (company) session.get (Company.class, 1); Company.setcompanyname ("Santa Monica"); Session.update ( Company); Session.flush (); A company Newcompany = new company (); Newcompany.setcompanyname ("UBISOFT"); Session.save (newcompany); transaction.co Mmit (); Bsession.close ();

A: An UPDATE statement will be issued here immediately to update the company information. The data is in an uncommitted state.

B: An INSERT statement is issued here to update the database. The data is in an uncommitted state.


Note: When hibernate uses a primary key generation policy for UUID, we change the type of the CompanyID property of the company entity class to string and modify the company HBM configuration to <generator class= " UUID ">. The following test methods are also used:

Company Company = (company) session.get (Company.class, 1); Company.setcompanyname ("Santa Monica"); Session.update ( Company); Acompany Newcompany = new Company (), Newcompany.setcompanyname ("UBISOFT"); Session.save (Newcompany); Btransaction.commit (); Csession.close ();

A, B: Both update () and save () do not send SQL statements to the database, and the Save () method in the first Test sends the SQL statement because we are using the native primary key generation strategy, and to get the primary key you must first insert the data into the database. The UUID is generated by hibernate itself, so only the cache is added and no SQL statements are emitted.

C: This place is critical , let's look at the SQL statements generated by hibernate:

INSERT INTO Company (company_name, company_id) VALUES (?,?)  Update company set company_name=? where company_id=?

As can be seen from the above: Hibernate when using objects in the cache to manipulate the database, not in accordance with the order of our program to execute, but first execute insert, and then execute update and delete. This is likely to make the results of the implementation different than we expected. At this point, you should use the flush () method to manually let hibernate send SQL statements in a timely manner. In the example above, if we add the Session.flush () method after the a step, then update will be executed before insert.

This article is from the "Bronze Gong" blog, please be sure to keep this source http://jaeger.blog.51cto.com/11064196/1757270

Evict, clear, and flush methods for hibernate 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.