Hibernate Iterate List Differences

Source: Internet
Author: User

Iterate:

	        String sql = "from Bigque where ID <= 5";
		String sql2 = "from Bigque where ID <= 6";
		Bigquedao dao = new Bigquedao ();
		Session session = Dao.getsession ();
		Iterator<bigque> it = session.createquery (sql). Iterate ();
		
		while (It.hasnext ()) {
			System.out.println (It.next (). getcontent ());
		}
		
		System.out.println ("===========");
		
                Iterator<bigque> it2 = Session.createquery (SQL2). Iterate ();
		
		while (It2.hasnext ()) {
			System.out.println (It2.next (). getcontent ());
		}
		Session.close ();

Results:

Hibernate:select bigque0_.id as col_0_0_ from Cloudroom.bigque bigque0_ where bigque0_.id<=5 hibernate:select bigque0 _.id as id51_0_, bigque0_. Content as content51_0_, bigque0_.
Subjectid as subjectid51_0_ from Cloudroom.bigque bigque0_ where bigque0_.id=? Select Hibernate:select bigque0_.id as id51_0_, bigque0_. Content as content51_0_, bigque0_.
Subjectid as subjectid51_0_ from Cloudroom.bigque bigque0_ where bigque0_.id=? The blank question Hibernate:select bigque0_.id as id51_0_, bigque0_. Content as content51_0_, bigque0_.
Subjectid as subjectid51_0_ from Cloudroom.bigque bigque0_ where bigque0_.id=? Reading questions Hibernate:select bigque0_.id as id51_0_, bigque0_. Content as content51_0_, bigque0_.
Subjectid as subjectid51_0_ from Cloudroom.bigque bigque0_ where bigque0_.id=? Judgment Hibernate:select Bigque0_.id as id51_0_, bigque0_. Content as content51_0_, bigque0_.
Subjectid as subjectid51_0_ from Cloudroom.bigque bigque0_ where bigque0_.id=? Select =========== hibernate:select bigque0_.id as col_0_0_ from ClouDroom.bigque bigque0_ where bigque0_.id<=6 selection questions the question of reading question question choice Hibernate:select bigque0_.id as id51_0_, bigque0_. Content as content51_0_, bigque0_.
Subjectid as subjectid51_0_ from Cloudroom.bigque bigque0_ where bigque0_.id=? Blank questions


Conclusion:

Lazy loading sends an SQL statement first to get all the IDs used and send SQL to the database to find each object


Two times in a session iterate the second iterate will still send a SQL get ID used to send SQL to the database to find each object (if there is this object in the session, it is found in the cache, do not send SQL)


list:

                String sql = "from Bigque where ID <= 5";	String sql2 = "from Bigque where ID <= 6";
		Bigquedao dao = new Bigquedao ();
		Session session = Dao.getsession ();
		list<bigque> list = session.createquery (SQL). List ();
		for (int i=0; i<list.size (); i++) {
			System.out.println (List.get (i). GetContent ());
		}
		System.out.println ("=============");
		
		List<bigque> list2 = session.createquery (SQL). List ();
		for (int i=0; i<list2.size (); i++) {
			System.out.println (List2.get (i). GetContent ());
		}
		Session.close ();
Results:
Hibernate:select bigque0_.id as Id51_, bigque0_. Content as Content51_, bigque0_. Subjectid as subjectid51_ from Cloudroom.bigque bigque0_ where bigque0_.id<=5 choosing the questions of
reading
Questions in the blank question Select
=============
hibernate:select bigque0_.id as id51_, bigque0_. Content as Content51_, bigque0_. Subjectid as subjectid51_ from Cloudroom.bigque bigque0_ where bigque0_.id<=5 choosing the questions of reading questions in the
blank question
Select question
Conclusion: List fetch all the second issue will still go to the database to find


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.