One. Lazy loading.
Why use lazy loading?
It is an optimization method to load this mode when data is needed.
Lazy loading of three types of lazy loading (a) in Hibernate
@Test publicvoid Testclass_lazy () { sessionfactory sessionfactory =sessionfactoryutils.getsessionfactory (); Session session=sessionfactory.opensession (); Privilege Privilege= (Privilege) session.load (Privilege. Class// did not issue SQL statement privilege.getname (); Issue SQL Statement session.close () when the property is actually loaded ; }
See that the privilege object at this point is a proxy object (Javassist We have imported this jar package, knowing that the package was created as a proxy object.) The proxy object finally returns a privilege type Object, which indicates that the proxy object is a subclass of the target class (the privilege Class).
Conclusion: Session.load (..) The resulting proxy object, which is a subclass of the persisted class.
(b). Lazy Loading of collections
@Test publicvoid testclass_lazy2 () { sessionfactory sessionfactory =sessionfactoryutils.getsessionfactory (); Session session=sessionfactory.opensession (); Privilege Privilege= (Privilege) session.get (Privilege. Class, 2); Set<Person> persons=privilege.getpersons (); for (person p:persons) { System.out.println (P.getname ()); } Session.close (); }
Description
1. Because the delay is loaded in the mapping file, and once the mapping file is determined that it cannot be modified
2. Lazy loading increases efficiency by controlling the time the SQL statement is issued
- Lazy Loading of Many-to-one (single-ended association)
Load one end according to the multiple end
Why is it that many of these parties load one side of an unknown?
There's no effect on performance, because there's only one piece of data, so it's okay.
Two. Crawl strategy
Why do I need to use a crawl strategy?
Question: When querying a one-to-many relational object, how to query the most efficient! (Classes-Student)
Method One: Query classes The number of records, and then according to the query out of the data to student find the corresponding data, according to (C.ID=S.CID). So it caused the N+1 query statement. N:classes number of records 1:classes itself
Method Two: Use iterator (). To query will also cause N+1 query statement
Method Three: Modify the crawl strategy, query only need to query the message
Summary: The problem that the crawl strategy solves is how to load the data in the set collection, issue the SQL statement, and load the data that loads the set.
Using crawl policies
Because the crawl strategy solves the problem of the data in the set collection, in the set element of the mapping file, you can set the crawl policy, specifically
First step: Set the Fetch property in the mapping file
<name= "Persons" cascade= "Save-update" fetch= " Join ">.... </ Set >
The value of the Fetch attribute:
Join: Left OUTER join: Use a connection query (one-to-many relationship) two tables of data (PS: If there is a subquery can not use the join, this time becomes a select)
(default) Select: Queries all objects of one party first, and then queries the associated object based on the ID value of each object
Subselect: Support subquery
The second step: still take the above method one and method two query method, observe the SQL statement issued by the background
Hibernate:
Select
Students0_.cid as Cid0_1_,
Students0_.id as Id1_,
Students0_.id as id1_0_,
Students0_.name as Name1_0_
From
STUDENT students0_
where
Students0_.cid in (
Select
Classes0_.id
From
CLASSES classes0_
)
This subquery improves efficiency.
Description
1. Because the crawl policy is set in the mapping file, it cannot be modified once the map file is generated.
2. Increase efficiency by issuing what kind of SQL statement to load the collection
Hibernate supports three types of queries:
1.HQL statements
2. Conditions (criteria) inquiry
Class stitching, viewing hibernate source documents
3.sql Query
Hibernate "Performance section"