Hibernate Performance Optimization

Source: Internet
Author: User

1. Apply the <dynamc-Insert> and <dynamic-Update> attributes of the <class> label.

Problem: The <dynamc-Insert> and <dynamic-Update> attributes are not used. By default, hql initiated by update or insert updates all fields.

For example, book = hibernatedao. Load (ID );
Book. setname ("Chen ");
Background hql update book set name = ?, Price = ?, Date =? Where id = ?;
When the <dynamc-Insert> and <dynamic-Update> attributes are set to true, new or updated fields are set as needed,
Background hql update book set name =? Where id = ?;

 

Ii. Delayed Loading

(1) Delayed loading of Object

Configuration File Settings

<Hibernate-mapping>

<Class name = "com.cn... Tuser"

Table = "Tuser"

Lazy = true>

</Hibernate-mapping>

Tuser user = session. Load (Tuser. Class, new INTEGER (2) ---- (1)

User. getname () ------ (2)

The configuration file lazy = true is set to delayed loading. hibernate performs database query only when the client calls the Value Method of the object class (Execution (2. During execution (1), a proxy class is constructed first, and the target class is placed in the target attribute of the proxy class. All its attributes are null and logs are not printed, the SQL statement has not been executed.

If lazy = false, logs are printed when the program runs to (1). It indicates that hibernate has retrieved records from the database table and constructed a complete Tuser object.

(2) Delayed loading of Sets

Configuration File

<Hibernate-mapping>

<Class name = "com.cn... Tuser"

Table = "Tuser"

>

<Set name = "Address"

Table = "Tuser"

Layz = true

Casecade = "all" // all operations are associated with the Operation Save update Delete

Inverse = "false"> // maintain the relationship between the two entities. If the relationship is false, the object is maintained by the other party.

<Key column = "tuser_id"> </key>

<One-to-learn class = "cn.com... address"> </one-to-learn>

<Set>

</Hibernate-mapping>

Eg:

Tuser user = (Tuser) Session. Load (Tuser. Class, new INTEGER (2 ))

Set addset = user. getaddress () ------- (1)

Iterator = addset. ietrator (); -------- (2)

While (iterator. hasnext ()){}

When the program is executed to (1), the address data has not been read, but it is only a net. SF. hibernate. collection. Set instance.

Data is actually loaded only when the execution reaches (2). This is the delayed loading mechanism of the set.

(3) Delayed attribute Loading

<Hibernate-mapping>

<Class name = "com.cn... Tuser"

Table = "Tuser"

>

<Property

Name = "resume"

Type = "Java. Lang. Integer"

Column = "resume"

Lazy = "true"

/>

</Hibernate-mapping>

 

While (iterator. hasnext ()){

User. getname ();

User. getresume ();

}

The log executes two SQL statements. The first statement loads the non-delayed loading field, and the second statement loads the delayed loading field.

Iii. Capture Policy
1. The default fetch policy is select.
<Manyh-to-one name = "classes" column = "classesid" Fetch = "select"/> Similarly, the set is also <set name = "Students" cascade = "all" Fetch = "select"> <key column = "classid"/> <one-to-convert class = "cn.com. student "/> </set>
With the exception, an SQL statement is sent to capture the entity or set associated with the current object, which takes two days in total.
2. Fetch = "join": A total of SQL statements are generated. The associated object or set is directly loaded through the left-out left Outer Join. In this case, lazy becomes invalid.
3. The set fetch = "Subselect" mainly applies to hql
List list = session. createquery ("select C from classes C where c. ID in (1, 2, 3)"). List ();
For (iterator = List. iterator (), iterator. hasnext ()){
Classes classes = (classes) iterator. Next ();
System. Out. println ("classes. Name =" + classes. getname ());
For (iterator iterator1 = classes. getstudents (). iterator (); iterator1.hasnext ()){
Student = (student) iterator1.next ();
System. Out. println ("student. Name =" + student. getname ());
}
}
If fetch = "select" is set for hql one-to-minus, an SQL statement is sent every time a class is identified to find the corresponding class.
Log: hibernate: Select classes0 _. ID from t_class classes0 _ Where classes0 _. ID in (1, 2, 3 );
Classes. Name = "Class 0 ";
Hibernate: Select student0 _. ID, student0 _. name from t_student student0 _ Where student0 _. classesid =? // Locate
If fetch = "Subselect" is set, the corresponding students in the class are checked through the subquery.
Hibernate: Select student0 _. ID, student0 _. name from t_student student0 _ Where student0 _. classesid in (select classes0 _. ID from t_class classes0 _ Where classes0 _. ID in (1, 2, 3 ));
4. Batch loading of entity batch-size with group and Optimized Performance
List list = session. createquery ("select s from student where S. ID in (: IDS)"). List ()
. Setparameterlist ("IDS", new object });
. List ();
For (iterator = List. iterator (); iterator. hasnext ()){
Student = (student) iterator. Next ();
System. Out. println ("student. Name =" + student. getname ());
System. Out. println ("student. classes. Name =" + student. getclasses. getname ());
}
Each cycle sends an SQL statement to query the student's class. Therefore, 10 such SQL statements are sent.
Log: Select classes0 _. ID, classes0 _. name from t_classes classes0 _ Where classes0 _. ID =?
In the classes. HBM. xml file, <class name = "classesimpl" table = "t_classes" batch-size = 5>
Log: Select classes0 _. ID, classes0 _. name from t_classes classes0 _ Where classes0 _. ID in (?,?,?,?,?);
5. Load a set in batches
Similarly, in <set name = "Students" batch-size = "5" cascade = "all">
<Key column = "classesid">
<One-to-register classes = "cn.com. Student"/>
</Set>

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.