In the hibernate Series Article The first article mentioned that using hibernate has a performance problem, but everything is not absolute. There will always be a way. Although the following methods cannot completely solve the performance problem, but it can basically meet most of the requirements.
Capture Policy
Single-ended proxy
A) Keep the default value, which is the same as fetch = "select", for example, <subtitle-to-onename = "classes" column = "classesid" Fetch = "select"/>, fetch = "select". In addition, a SELECT statement is sent to load the associated object or set of the current object.
B) Set fetch = "join", for example, <subtitle-to-onename = "classes" column = "classesid" Fetch = "join"/>, hibernate uses a SELECT statement to connect (Inline/external: depends on whether the foreign key is empty) to capture its associated objects or sets. Lazy is invalid. If fetch is select or join, hql queries are not affected, it affects the load and get methods.
<Fail-to-one> the n + 1 problem may occur. For example, to query the list of 100 students, the SQL statement for querying the students is first issued, then, the SQL statement for querying the class based on the class ID will be issued, which will lead to the n + 1 problem, that is, the n + 1 Statement will be issued, which will seriously affect the performance, therefore, we can adopt pre-capturing policies, such as: selects from student s join fetch S. classes.
Set proxy
A) Keep the default value, which is the same as fetch = "select", for example: <setname = "Students" Order-by = "ID" inverse = "true" cascade = "all" Fetch = "select">, fetch = "select ", in addition, a SELECT statement is sent to load the associated object or set of the current object.
B) Set fetch = "join", for example: <setname = "Students" Order-by = "ID" inverse = "true" cascade = "all" Fetch = "join">, hibernate uses a SELECT statement to join (Inline/external) to capture its associated objects or sets. Fetch = "join" means lazy is invalid. Fetch = "join ", only get and load are affected, but hql is not affected.
C) Set fetch = "Subselect", for example: <setname = "Students" Order-by = "ID" inverse = "true" cascade = "all" Fetch = "Subselect">, in addition, a SELECT statement is sent to capture the association set of all objects previously queried. Fetch = "Subselect" affects hql queries.
Batch capture
Batch-size attribute. you can load entity classes in batches. For more information, see classes. HBM. configuration in XML: <classname = "com. bjpowernode. hibernate. classes "table =" t_classes "batch-size =" 10 "> the batch-size attribute is used to reduce the amount of SQL statements sent.
Lazy Loading Mechanism
Lazy is a delayed loading task. It is created only when this object is actually used. For hibernate, SQL is issued only when it is actually used, which improves some performance. Hibernate lazyloading uses a hibernatesession to manage a session. Its logic is to open a new session every time a database operation is performed. After the operation is completed, the session is closed immediately, the advantage of doing so is that the session can be strictly closed, but it is not suitable for cross-method transactions.
Lazy on the class label
<Class> label; optional values: True/false, which only affects common attributes. Check the ID without sending an SQL statement, because the primary key is passed in. If you check other attributes, the SQL statement will be sent. The validity period of hibernatelazy must be when the session is open, the solution is to use opensessioninview (spring will explain later ).
Lazy on the set tag
<Set>/<list> label. The value can be true/false/extra. <Class> the lazy attribute on the tag does not affect the lazy attribute on the set. When the lazy attribute on the class tag is set to false, the common attribute is checked out when the class is loaded, but the set is not checked.
The get set does not send SQL statements, and the iteration sends SQL statements. When querying the number, the entire set is checked out, which affects the efficiency, lazy issues a count statement when getting the size with extra in the Set, improving the efficiency.
Lazy
<Allow-to-one>/<one-to-one> on a single-ended associated tag, you can set the value to false/Proxy/noproxy. <Class> tag lazy does not affect the lazy policy of single-ended associated objects. The lazy on a single-ended Association is the same as the set. When get is returned, the proxy does not issue a query statement, and an SQL statement is sent during use. Lazy = false on a single-ended Association. When you access a common attribute, two SQL statements are sent to query the attributes and corresponding correlated objects.