The difference between list and iterate in Hibernate hql

Source: Internet
Author: User

There are two ways to execute a HQL query, one is the list method and the other is the iterate method. What's the difference between the two methods, let's illustrate the difference by example.

Company table:

650) this.width=650; "src=" Http:// "title=" 1.PNG " alt= "Wkiol1bvhapare8eaaan7mngbzq433.png" style= "float:none;"/>

Employee table (employee_company_id as foreign key)

650) this.width=650; "src=" Http:// "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:

1. List method
The list method issues a SQL statement to the database, querying all records that satisfy the HQL statement at a time, and testing the method as follows:

list<employee> allemployees = Session.createquery ("from Employee"). List ();    Afor (Employee employee:allemployees) {System.out.println (Employee.getemployeeid ()); System.out.println (Employee.getemployeename ());}

A: This will issue SQL to query all employee information that satisfies the HQL condition, SQL is as follows:

Select employee0_.employee_id as employee1_1_, employee0_.employee_name as employee2_1_, Employee0_.employee_c ompany_id as employee3_1_ from employee employee0_

2. Iterate method

The iterate method only queries the primary key of the table to return the iterator object, and when we actually use the object, we use the primary key to go to the database to find other information, the test method is as follows:

iterator<employee> allemployees = Session.createquery ("from Employee"). Iterate ();    Awhile (Allemployees.hasnext ()) {Employee employee = (); System.out.println (Employee.getemployeeid ()); B System.out.println (Employee.getemployeename ()); C

A: This will first query all the primary keys of the employee table, SQL is as follows:

Select employee0_.employee_id as col_0_0_ from employee employee0_

B: This will return the employee ID, but will not issue the SQL statement to the database, because the employee ID is the primary key, the above has been queried, using not to go to the database query.

C: The database will be queried for additional information again based on each employee ID.

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=?

Summary: The list method will only be queried once, and the Iterate method will first check all primary keys, and then traverse the primary key to query other information, so a total of query n+1 times. When our program is based on the primary key to do different processing, such as: if (employee.getemployeeid () = = 1), should use iterate, because only the primary key will not be sent SQL statements, will also make the loading data greatly reduced. However, if you want to traverse all the information, you should use the list method, you can load all the information at once, without frequent communication with the database.

This article is from the "Bronze Gong" blog, please be sure to keep this source

The difference between list and iterate in Hibernate hql

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: 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.