Hibernate learning Notes (eight)-lazy loading and crawling strategies

Source: Internet
Author: User

Lazy Loading (load on Demand) is a unique and powerful method of data acquisition, which can automatically get more data when the user scrolls the page, while the new data will not affect the display of the original data, and minimize the resource consumption on the server side. Summary sentence: When to need data, when to load.

First, lazy loading

Lazy loading of Class 1.1


The proxy class generated by Javassist and the classes class are inheritance relationships,

session.load () method produces a proxy object , which is a subclass of the persisted class

/** * Class Lazy loading * * @Testpublic void Testclass_lazy () {session = Hibernateutils.opensession (); transaction = Session.begintransaction (); Classes Classes = (Classes) session.load (Classes.class, 1L);//classes.setname ("Asdj"); System.out.println (Classes.getname ());//Issue sqltransaction.commit (); Session.close ();}

1.2 Lazy loading of the collection

/** * Collection of lazy loading */@Testpublic void Testcollect_lazy () {session = Hibernateutils.opensession (); transaction = Session.begintransaction (); Classes Classes = (Classes) session.get (Classes.class, 1L); set<student> students = classes.getstudents ()//Not issued for (Student student:students) {// SqlSystem.out.println (Student.getname ()) is emitted during iteration; Transaction.commit (); Session.close ();} /** * Further lazy loading, set extra, for query rows, maximum, minimum, etc./@Testpublic void Testlazy_extra () {session = Hibernateutils.opensession (); Transaction = Session.begintransaction (); Classes Classes = (Classes) session.get (Classes.class, 1L); set<student> students = classes.getstudents (); System.out.println (Students.size ()); Transaction.commit (); Session.close ();}

1.3 Lazy loading of single-ended associations (lazy loading of many2one)

Depending on the one end of the load, the fact is that just a little more data is being checked, which has little effect on performance, such as checking the class according to the students.

Summarize

Lazy loading is an optimization method provided by Hibernate.

1, delay loading in the mapping file settings, and once the mapping file is determined, cannot be modified.

2, lazy loading is to improve efficiency by controlling the timing of SQL statements.

So in the development of the general is not to set the lazy load configuration.

Second, crawl strategy

The crawl strategy solves the problem of how to load the data in the Set collection


public class Fetchtest {private Session session;private Transaction transaction;private sessionfactory sessionfactory;/ * * Fetch:select Case: * First find classes ID, in accordance with ID student * N+1 data * n:classes table in the number of * 1:classes table itself */@Testpublic void Testquery Allstudent () {session = Hibernateutils.opensession (); list<classes> Classes = Session.createquery ("from Classes"). List (); for (Classes classes2:classes) { System.out.println (Classes2.getname ()); set<student> students = classes2.getstudents (); for (Student student:students) {System.out.println ( Student.getname ());}} Session.close ();} /** * Fetch:subselect Situation: * Because the requirement contains subqueries, FETCH uses subselect * 2 SQL */@Testpublic void TestQueryAllStudent2 () {session = H Ibernateutils.opensession (); list<classes> Classes = Session.createquery ("from Classes"). List (); for (Classes classes2:classes) { System.out.println (Classes2.getname ()); set<student> students = classes2.getstudents (); for (Student student:students) {System.out.println ( Student.getname());}} Session.close ();} /** * SELECT * First queries all objects of one party (Classes), and then queries its associated object (Student) based on the ID value of each object. */@Testpublic void Testqueryclassesbycidandstudents_select () {Session session = Hibernateutils.opensession (); Classes Classes = (Classes) session.get (Classes.class, 1L); System.out.println (Classes.getname ()); set<student> students = classes.getstudents (); for (Student student:students) {System.out.println ( Student.getname ());} Session.close ();} /** * Join uses the LEFT OUTER join an SQL statement to query all classes and student tables. * * In demand analysis with subqueries, the FETCH strategy using JOIN IS not taken * 1 SQL */@Testpublic void TESTQUERYCL Assesbycidandstudents_join () {Session session = Hibernateutils.opensession (); Classes Classes = (Classes) session.get (Classes.class, 1L);//issuing SQL, so fetch as join crawl, will cause lazy load failure System.out.println ( Classes.getname ()); set<student> students = classes.getstudents (); for (Student student:students) {System.out.println ( Student.getname ());} Session.close ();}}


Summarize:

Since the setting of the crawl policy is in the mapping file, it cannot be changed once the map file is generated.

The collection is loaded by issuing different forms of the SQL statement, thereby optimizing efficiency.

fetch as Join's crawl strategy will cause lazy loading to fail

When set to join, he will directly query from table information to join instead of using select query, which causes lazy loading to fail.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hibernate learning Notes (eight)-lazy loading and crawling strategies

Related Article

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.