Hibernate framework Learning (10)-query optimization, hibernate framework
I. Class-level Query
1. get method: If there is no policy, the database load data is queried immediately after the call.
2. load Method: when an object is returned without sending any SQL statement, the query is executed only when the object is used. The load policy at the application class level is applied.
1> delayed loading: only obtained, not used, not queried, and used for query
2> whether to perform delayed loading: You can configure the lazy attribute on the class element to control
Lazy (default): When the Query Class is true, the proxy object is returned. When the attribute is used, the database is queried based on the associated session and the data is loaded.
It is not queried during loading. It is only queried during use.
Lazy: Query immediately when loading with false, no difference with get Method
Conclusion: To improve efficiency, it is recommended to use delayed loading (lazy loading)
3> Note: Make sure that the session is opened when calling the property to load data. Otherwise, an exception is thrown.
2. Association-level Query
1. Set policies
Lazy attribute: determines whether to delay Loading
True (default): delayed loading and lazy loading
False: load immediately
Extra: extremely lazy, basically consistent with lazy loading
Fetch attribute: determines the load policy and type of SQL statements used to load set data.
Select (default): Single Table query Loading
Join: use multi-table queries to load a set
Subselect: Use a subquery to load a set
2. Associate an attribute Policy
Lazy attribute: determines the loading time
False: load immediately
Proxy: determined by the class-level loading policy of the Customer
Fetch attribute: determines the loaded SQL statement
Select: query using a single table
Join: Multi-Table query
3. Conclusion
To improve efficiency, select fetch, and select true for lazy. The default value is used for all fetch operations.
4. no-session Solution
Expand the scope of the session
3. Batch capture
Batch-size: Number of captured Sets
When capturing a customer's set, the customer's set of contacts is captured at a time.