The difference between the list_iterate of "hibernate framework" performance optimization

Source: Internet
Author: User
Tags commit
The difference between list and iterate:

Or use the last example, Topic topic and Section Category
Topic.java:
Package com.bjsxt.hibernate;
Import java.util.ArrayList;
Import Java.util.Date;

Import java.util.List;
Import javax.persistence.Entity;
Import Javax.persistence.FetchType;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;
Import Javax.persistence.ManyToOne;
Import javax.persistence.NamedQueries;
Import Javax.persistence.NamedQuery;
Import Javax.persistence.OneToMany;
Import javax.persistence.Temporal;

Import Javax.persistence.TemporalType;

Import org.hibernate.annotations.BatchSize;
	@Entity public class Topic {private int id;
	Private String title;
	Private category category;
	Private Date CreateDate;
	
	Private list<msg> msgs = new arraylist<msg> ();
	@OneToMany (mappedby= "topic") Public list<msg> Getmsgs () {return msgs;
	} public void Setmsgs (list<msg> msgs) {this.msgs = msgs;
	} @Temporal (temporaltype.time) public Date getcreatedate () {return createdate; } public void Setcreatedate (Date createdate) {This.createdaTe = CreateDate;
	} @ManyToOne (Fetch=fetchtype.lazy) public category GetCategory () {return category;
	} public void Setcategory (category category) {this.category = category;
	} @Id @GeneratedValue public int getId () {return Id;
	} public void setId (int id) {this.id = ID;
	} public String GetTitle () {return title;
	public void Settitle (String title) {this.title = title; }
	
}

Category.java:
Package com.bjsxt.hibernate;


Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;

Import org.hibernate.annotations.BatchSize;

@Entity public
class Category {
	private int id;
	private String name;
	@Id
	@GeneratedValue Public
	int getId () {
		return Id;
	}
	public void setId (int id) {
		this.id = ID;
	}
	Public String GetName () {
		return name;
	}
	public void SetName (String name) {
		this.name = name;
	}
}

Add data (each plate CI has a topic ti)
@Test public
	void Testsave () {
		Session session = Sf.opensession ();
		Session.begintransaction ();
		
		Each section CI has a topic under TI for
		(int i=0; i<10; i++) {
			Category C = new category ();
			C.setname ("C" + i);
			Topic t=new Topic ();
			T.setcategory (c);
			T.settitle ("T" +i);
			T.setcreatedate (New Date ());
			Session.save (c);
			Session.save (t);
		}


		Session.gettransaction (). commit ();
		Session.close ();
	}

Here are the different tests for list and iterate

1.list Take All
@Test public
	void TestOneAddNProblem1 () {
		Session session = Sf.opensession ();
		Session.begintransaction ();
		List<topic> topics= (list<topic>) session.createquery ("from Topic"). List ();
		for (Topic t:topics) {
			System.out.println (T.getid () + "-" +t.gettitle ());
		}
		Session.gettransaction (). commit ();
		Session.close ();
		
	}
Results:
Hibernate:
Select
Topic0_.id as Id2_,
topic0_.category_id as Category4_2_,
Topic0_.createdate as Createdate2_,
Topic0_.title as Title2_
From
Topic topic0_
1-t0
2-t1
3-t2
4-t3
5-t4
6-t5
7-t6
8-t7
9-t8
10-t9

2.iterate takes the ID first, and so on when it is used to take the object by ID.
@Test public
	void Testlistanditerate () {
		Session session = Sf.opensession ();
		Session.begintransaction ();
		Iterator<topic> topics= (iterator<topic>) session.createquery ("from Topic"). Iterate ();
		For (Topic T=topics.next (); T!=null;t=topics.next ()) {
			System.out.println (T.getid () + "-" +t.gettitle ());
		}
		Session.gettransaction (). commit ();
		Session.close ();
	}
Test results:
Hibernate:
Select
Topic0_.id as Col_0_0_
From
Topic topic0_
Hibernate:
Select
Topic0_.id as id2_0_,
topic0_.category_id as category4_2_0_,
Topic0_.createdate as createdate2_0_,
Topic0_.title as Title2_0_
From
Topic topic0_
where
Topic0_.id=?
1-t0
Hibernate:
Select
Topic0_.id as id2_0_,
topic0_.category_id as category4_2_0_,
Topic0_.createdate as createdate2_0_,
Topic0_.title as Title2_0_
From
Topic topic0_
where
Topic0_.id=?
2-t1
Hibernate:
Select
Topic0_.id as id2_0_,
topic0_.category_id as category4_2_0_,
Topic0_.createdate as createdate2_0_,
Topic0_.title as Title2_0_
From
Topic topic0_
where
Topic0_.id=?
3-t2
Hibernate:
Select
Topic0_.id as id2_0_,
topic0_.category_id as category4_2_0_,
Topic0_.createdate as createdate2_0_,
Topic0_.title as Title2_0_
From
Topic topic0_
where
Topic0_.id=?
4-t3
Hibernate:
Select
Topic0_.id as id2_0_,
topic0_.category_id as category4_2_0_,
Topic0_.createdate as createdate2_0_,
Topic0_.title as Title2_0_
From
Topic topic0_
where
Topic0_.id=?
5-t4
Hibernate:
Select
Topic0_.id as id2_0_,
topic0_.category_id as category4_2_0_,
Topic0_.createdate as createdate2_0_,
Topic0_.title as Title2_0_
From
Topic topic0_
where
Topic0_.id=?
6-t5
Hibernate:
Select
Topic0_.id as id2_0_,
topic0_.category_id as category4_2_0_,
Topic0_.createdate as createdate2_0_,
Topic0_.title as Title2_0_
From
Topic topic0_
where
Topic0_.id=?
7-t6
Hibernate:
Select
Topic0_.id as id2_0_,
topic0_.category_id as category4_2_0_,
Topic0_.createdate as createdate2_0_,
Topic0_.title as Title2_0_
From
Topic topic0_
where
Topic0_.id=?
8-t7
Hibernate:
Select
Topic0_.id as id2_0_,
topic0_.category_id as category4_2_0_,
Topic0_.createdate as createdate2_0_,
Topic0_.title as Title2_0_
From
Topic topic0_
where
Topic0_.id=?
9-t8
Hibernate:
Select
Topic0_.id as id2_0_,
topic0_.category_id as category4_2_0_,
Topic0_.createdate as createdate2_0_,
Topic0_.title as Title2_0_
From
Topic topic0_
where
Topic0_.id=?
10-t9
So we can see, first take the ID, and so on, when used to take the object by ID.

The list is issued for the second time in 3.session and will still be queried by the database.
Test code:
@Test public
	void TestListAndIterate2 () {
		Session session = Sf.opensession ();
		Session.begintransaction ();
		Iterator<topic> topics= (iterator<topic>) session.createquery ("from Topic"). Iterate ();
		Query q=session.createquery ("from Topic");
		List<topic> topics=q.list ();
		for (Topic t:topics) {
			System.out.println (T.getid () + "-" +t.gettitle ());
		}
		List<topic> topics2=q.list ();
		for (Topic t:topics2) {
			System.out.println (T.getid () + "-" +t.gettitle ());
		}
		Session.gettransaction (). commit ();
		Session.close ();
	}
Test results:
Hibernate:
Select
Topic0_.id as Id2_,
topic0_.category_id as Category4_2_,
Topic0_.createdate as Createdate2_,
Topic0_.title as Title2_
From
Topic topic0_
1-t0
2-t1
3-t2
4-t3
5-t4
6-t5
7-t6
8-t7
9-t8
10-t9
Hibernate:
Select
Topic0_.id as Id2_,
topic0_.category_id as Category4_2_,
Topic0_.createdate as Createdate2_,
Topic0_.title as Title2_
From
Topic topic0_
1-t0
2-t1
3-t2
4-t3
5-t4
6-t5
7-t6
8-t7
9-t8
10-t9

4.iterate the second time, first find session level cache
Test code:
@Test public
	void TestListAndIterate3 () {
		Session session = Sf.opensession ();
		Session.begintransaction ();
		iterator<category> categories = (iterator<category>) session.createquery ("from Category"). Iterate ();
		
		while (Categories.hasnext ()) {
			Category c = categories.next ();
			System.out.println (C.getname ());
		}
		
		Iterator<category> categories2 = (iterator<category>) session.createquery ("from Category"). Iterate ();
		
		while (Categories2.hasnext ()) {
			Category c = categories2.next ();
			System.out.println (C.getname ());
		}
		Session.gettransaction (). commit ();
		Session.close ();
	}
Test results:
Hibernate:
Select
Category0_.id as Col_0_0_
From
Category category0_
Hibernate:
Select
Category0_.id as id0_0_,
Category0_.name as Name0_0_
From
Category category0_
where
Category0_.id=?
C0
Hibernate:
Select
Category0_.id as id0_0_,
Category0_.name as Name0_0_
From
Category category0_
where
Category0_.id=?
C1
Hibernate:
Select
Category0_.id as id0_0_,
Category0_.name as Name0_0_
From
Category category0_
where
Category0_.id=?
C2
Hibernate:
Select
Category0_.id as id0_0_,
Category0_.name as Name0_0_
From
Category category0_
where
Category0_.id=?
C3
Hibernate:
Select
Category0_.id as id0_0_,
Category0_.name as Name0_0_
From
Category category0_
where
Category0_.id=?
C4
Hibernate:
Select
Category0_.id as id0_0_,
Category0_.name as Name0_0_
From
Category category0_
where
Category0_.id=?
C5
Hibernate:
Select
Category0_.id as id0_0_,
Category0_.name as Name0_0_
From
Category category0_
where
Category0_.id=?
C6
Hibernate:
Select
Category0_.id as id0_0_,
Category0_.name as Name0_0_
From
Category category0_
where
Category0_.id=?
C7
Hibernate:
Select
Category0_.id as id0_0_,
Category0_.name as Name0_0_
From
Category category0_
where
Category0_.id=?
C8
Hibernate:
Select
Category0_.id as id0_0_,
Category0_.name as Name0_0_
From
Category category0_
where
Category0_.id=?
C9
Hibernate:
Select
Category0_.id as Col_0_0_
From
Category category0_
C0
C1
C2
C3
C4
C5
C6
C7
C8
C9
See the second use of the session cache in the data, and do not go to the database to check.

Summarize:
The reason is: First look at the List,list take 10 data after the session has 10 data cache, the second list to fetch, it still has to go to the database to load once, and then refresh the cache, and then the original object to re-brush again. That is, the list does not read the session cache, because as a query, query conditions are difficult to identify, not the list does not want to use, but difficult to use.
Iterate is not the same, first in the first time, he first took the ID out, because the back we did the traversal, so he put the ID corresponding to the object is all in the cache, the second time to do the traversal, this time as long as the cache to check the line.
When to use list. When to use iterate. You can use list on the job.
Tip: The first time you use list, the second time to visit the time you can use iterate. Because iterate will first check the cache, the cache has, it will not go to the database to check.

Main Coping Interview

Reprint Please specify source: http://blog.csdn.net/acmman/article/details/43937591

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.