Hibernate framework study notes 12: Query Optimization, hibernate Study Notes
Class-level query optimization:
Create an object class:
Package domain; import java. util. hashSet; import java. util. set; // Customer entity public class Customer {private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_linkman; private String cust_phone; private String cust_mobile; // use the set Set to express one-to-multiple relationships. private Set <LinkMan> linkMens = new HashSet <LinkMan> (); public set <LinkMan> getLinkMens () {return linkMens;} public void setLinkMens (Set <LinkMan> linkMens) {this. linkMens = linkMens;} public Long getCust_id () {return cust_id;} public void setCust_id (Long cust_id) {this. cust_id = cust_id;} public String getCust_name () {return cust_name;} public void setCust_name (String cust_name) {this. cust_name = cust_name;} public String getCust_source () {return cust_source;} public void setCust_source (String cust_source) {this. cust_source = cust_source;} public String getCust_industry () {return cust_industry;} public void setCust_industry (String cust_industry) {this. cust_industry = cust_industry;} public String getCust_level () {return cust_level;} public void setCust_level (String cust_level) {this. cust_level = cust_level;} public String getCust_linkman () {return cust_linkman;} public void setCust_linkman (String cust_linkman) {this. cust_linkman = cust_linkman;} public String getCust_phone () {return cust_phone;} public void setCust_phone (String cust_phone) {this. cust_phone = cust_phone;} public String getCust_mobile () {return cust_mobile;} public void setCust_mobile (String cust_mobile) {this. cust_mobile = cust_mobile;} @ Override public String toString () {return "Customer [cust_id =" + cust_id + ", cust_name =" + cust_name + "]" ;}}View Code
Custom tool class:
Package utils; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. cfg. configuration; public class HibernateUtils {private static SessionFactory sf; static {// 1 Creation, call the null parameter to construct Configuration conf = new Configuration (). configure (); // 2 create the SessionFactory object sf = conf Based on the configuration information. buildSessionFactory ();} // get session => get new session public static Session openSession () {// 3 get session Session session = sf. openSession (); return session;} // get session => get the bound session public static Session getCurrentSession () {// 3 get session Session session = sf. getCurrentSession (); return session ;}}View Code
Test example:
Package lazy; import org. hibernate. session; import org. hibernate. transaction; import org. junit. test; import domain. customer; import utils. hibernateUtils; // lazy loading | delays loading public class Demo {@ Test // get method: load immediately. send the SQL statement query result public void fun1 () {Session session = HibernateUtils immediately when executing the method. openSession (); Transaction tx = session. beginTransaction (); // ------------------------------------------------------ Customer c = session. get (Customer. class, 2l); System. out. println (c); // ------------------------------------------------------ tx. commit (); session. close () ;}@ Test // load method (default): No SQL statement is sent during execution. returns an object. query is performed only when this object is used. // delayed loading: only get unused. not queried. it is used only for query. // whether to load classes in a delayed manner: You can control the class by configuring the lazy attribute on the class Element in the configuration file. // lazy: when loading true, it is not queried. query B // lazy: false when loading. public void fun2 () {Session session = HibernateUtils. openSession (); Transaction tx = session. beginTransaction (); // ------------------------------------------------------ Customer c = session. load (Customer. class, 2l); // ------------------------------------------------------ tx. commit (); session. close (); System. out. println (c );}}
To improve efficiency, we recommend that you use delayed loading. Make sure that the session is opened when calling the attribute; otherwise, an exception is reported.
Association level optimization:
Configuration File: set attribute Configuration
Lazy attribute: determines whether to delay Loading
True (default): delayed loading and lazy loading
False: load immediately
Extra: extremely lazy
Fetch attribute: determines the loading policy. The type of SQL statement used to load the Set Data
Select (default): load a single table Query
Join: use multi-table queries to load a set
Subselect: Use a subquery to load a set
Conclusion: To improve efficiency, select. lazy for fetch selection should be set to true. All use the default value.