The difference between get () and load () in Hibernate queries, the difference between list () and iterate ()

Source: Internet
Author: User



The difference between get () and load () in the "Hibernate" query, the difference between list () and iterate ()



List () query

The data object is taken out at once @testpublic void Findtestlist () {Session s=sessionfactory.getcurrentsession (); S.begintransaction (); List<person> persons=s.createquery ("from"). List (), for (person person:persons) {System.out.println ( Person.getname () + "----" +person.getid ());} S.gettransaction (). commit ();}

The SQL statements are as follows:

16:00:43,118 DEBUG sql:111-select person0_.id as id1_, person0_.p_age as p2_1_, Person0_.group_ ID as group4_1_, person0_.p_name as p3_1_ from P_person person0_ 30----1 31----2 Sheets 32----3 Sheets 33----4 Sheets 34----5 35----6 36----7 37----8 Sheets 38----9 Sheets 39----10


Iterate () query


The primary key value of all objects is taken out at once, and the primary key values will be placed in the session buffer//the next time needed, directly in the session to obtain, do not need to access the database again//disadvantage is to query out the primary key values, and then the primary key to take the value of the object, Resulting in a huge database pressure @testpublic void findtestiterate () {Session s=sessionfactory.getcurrentsession (); S.begintransaction (); I Terator<person> persons=s.createquery ("from"). Iterate (), while (Persons.hasnext ()) {System.out.println (Persons.next (). GetName ());} S.gettransaction (). commit ();}


SQL statements

16:03:55,776 debug sql:111 -     select         person0_.id as col_0_0_     from         p_person person0_16:03:55,806 DEBUG SQL:111 -      select        person0_.id as id1_0_,         person0_.p_age as p2_1_0_,         person0_.group_id as group4_1_0_,        person0 _.p_name as p3_1_0_     from         p_person person0_     where         person0_.id=? Zhang San 016:03:55,823 debug sql:111 -     select         person0_.id as id1_0_,        person0_.p_age  as p2_1_0_,        person0_.group_id as group4_1_ 0_,        person0_.p_name as p3_1_0_      from        p_person person0_      where        person0_.id=? Zhang San 116:03:55,826 debug sql : 111 -     select        person0_.id  as id1_0_,        person0_.p_age as p2_1_0_,         person0_.group_id as group4_1_0_,         person0_.p_name as p3_1_0_     from         p_person person0_     where         person0_.id=? Zhang San 216:03:55,832 debug sql:111 -     select         person0_.id as id1_0_,         person0_.p_age as p2_1_0_,        person0_.group _id as group4_1_0_,        person0_.p_name as p3_1 _0_     from        p_person person0_      where        person0_.id=? Zhang San 316:0 3:55,836 debug sql:111 -     select         person0_.id as id1_0_,        person0_.p_ Age as p2_1_0_,        person0_.group_id as group4_1_0_,         person0_.p_name as p3_1_0_     from         p_person person0_     where         person0_.id=? Zhang San 416:03:55,838 debug sql:111 -      select        person0_.id as id1_0_,         person0_.p_age as p2_1_0_,         person0_.group_id as group4_1_0_,         person0_.p_name as p3_1_0_     from         p_person person0_     where         person0_.id=? Zhang San 516:03:55,841 debug sql:111 -     select         person0_.id as id1_0_,        person0_.p_age  as p2_1_0_,        person0_.group_id as group4_1_ 0_,        person0_.p_name as p3_1_0_      from        p_person person0_      where        person0_.id=? Zhang San 616:03:55,844 debug sql : 111 -     select        person0_.id  as id1_0_,        person0_.p_age as p2_1_0_,         person0_.group_id as group4_1_0_,         person0_.p_name as p3_1_0_     from         p_person person0_     where         person0_.id=? Zhang San 716:03:55,846 debug sql:111 -     select         person0_.id as id1_0_,         person0_.p_age as p2_1_0_,        person0_.group _id as group4_1_0_,        person0_.p_name as p3_1 _0_     from        p_person person0_      where        person0_.id=? Zhang San 816:0 3:55,875 debug sql:111 -     select         person0_.id as id1_0_,        person0_.p_age as p2_1_0_,         person0_.group_id as group4_1_0_,         person0_.p_name as p3_1_0_     from         p_person person0_     where         person0_.id= Zhang 39


Get () query

Send SQL statements immediately, whether we need it, such as Person.getname (), @Testpublic void Findtestget () {Session s=sessionfactory.getcurrentsession (); S.begintransaction (); Person person= (person) s.get (Person.class, 1);//system.out.println (Person.getname ()); S.gettransaction (). commit ();}


Load () query

SQL statements are not sent immediately, and SQL statements are sent only when we need them, such as person.getname (); @Testpublic void Findtestload () {Session s= Sessionfactory.getcurrentsession (); S.begintransaction (); Person person= (person) s.load (Person.class, 1);//system.out.println (Person.getname ()); S.gettransaction (). Commit () ;}


This article from "Promise always attached to the small wood 、、、" blog, please be sure to keep this source http://1936625305.blog.51cto.com/6410597/1576128

The difference between get () and load () in Hibernate queries, the difference between list () and iterate ()

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.