Previous post (lazy loading, lazily loading, lazy) when it comes to hibernate lazy loading and the configuration of the fetch, here's how to use the fetch.
Crawl strategy (Fetch) is the method by which the associated object is crawled when we are querying other objects that are associated with an object.
There are four types of fetch strategies: SELECT, Subselect, join, batch, as described below. We also use the related tables and entity classes that are described above for lazy loading.
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;"/>
Employee table (employee_company_id as foreign key)
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7C/A6/wKioL1bVHaPDb5pwAAAbt64pCpc595.png "title=" 2.PNG " alt= "Wkiol1bvhapdb5pwaaabt64pcpc595.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; }}
employee entity class:
public class employee { private int employeeid; private String employeeName; private Company Employeecompany; public int getemployeeid () { return employeeId; } public Void setemployeeid (Int employeeid) { This.employeeid = employeeid; } public string getemployeename () { return employeeName; } public void setemployeename (String employeeName) { this.employeeName = employeeName; } public Company getemployeecompany () { return Employeecompany; } public void setemployeecompany ( Company employeecompany) { this.employeeCompany = employeecompany; }}
company HBM configuration:
Employee HBM configuration:
The fetch attribute is used in the <many-to-one><one-to-many> and other related tags, we use the <many-to-one> to introduce the data from the database using the Get () method, so that the outer object without delay loading.
1. Select
When querying the associated object, the foreign key of the outer object is taken, and the SELECT statement is sent to the database to fetch the associated object.
Modify the configuration of the above employee HBM:
<many-to-one name= "Employeecompany" class= "Com.jaeger.hibernatetest.day7.lazy.Company" foreign-key= "Fk_empl Oyee_company "column=" employee_company_id "cascade=" save-update "fetch=" select "></many-to-one>
In the <many-to-one> tab we have added fetch= "Select", here is the test code:
Employee employee = (employee) session.get (Employee.class, 1); ASystem.out.println (Employee.getemployeename ()); Company Company = Employee.getemployeecompany (); BSystem.out.println (Company.getcompanyname ()); C
A: SQL statements are generated here to query employee information.
Select employee0_.employee_id as employee1_1_0_, employee0_.employee_name as employee2_1_0_, Employee0_.employ ee_company_id as employee3_1_0_ from employee employee0_ where employee0_.employee_id=?
B: There is no immediate access to company information, because the <many-to-one> in the default is lazy= "proxy" support lazy loading.
C: This will be based on the employee's foreign key to find company information, using the Select Fetch policy.
Select company0_.company_id as company_1_0_0_, company0_.company_name as company_2_0_0_ from 0_ where company0_.company_id=?
Note that:<many-to-one><one-to-many> and other associated tags are fetch= "select" By default, so we do not configure the use of multiple SELECT statements to query the associated object.
This article is from the "Bronze Gong" blog, please be sure to keep this source http://jaeger.blog.51cto.com/11064196/1748526
Hibernate Fetch fetching strategy