标签:select查询 col ESS none 方法 加载 action proxy alt
面试:Hibernate效率很低,如何优化?
缓存怎么弄的,语句怎么优化?
聊聊一级缓存,聊聊抓取策略和延迟加载,聊聊批量抓取
一般不单独使用,和延迟加载一起使用
延迟加载:lazy(懒加载)
执行到该行代码的时候不会发送语句,真正使用这个对象的属性的时候才会发送sql语句进行查询。
类级别延迟加载配置:默认为true
关联级别的延迟加载配置:在映射文件被关联的对象上配置
通过一个对象抓取到关联对象需要发送sql语句,sql语句如何发送,发送成什么格式通过策略进行配置
fetch:抓取策略,控制sql语句格式
lazy:延迟加载,控制查询关联对象的时候是否采用延迟
在实际开发中,一般都采用默认值。如果有特殊需求,可能配置join
各种配置的区别测试:
/** * 在<set>上的fetch和lazy */ public class HibernateDemo4 { @Test /** * 默认情况 */ public void demo1() { Session session = HibernateUtils.getCurrentSession(); Transaction transaction = session.beginTransaction(); // 查询1号客户 Customer customer = session.get(Customer.class, 1l);// 发送一条查询客户的sql System.out.println(customer.getCust_name()); // 查看1号客户每个联系人的信息 for (LinkMan linkMan : customer.getLinkMans()) {// 发送一条根据客户查询联系人的sql System.out.println(linkMan.getLkm_name()); } transaction.commit(); } @Test /** * 设置fetch=select,lazy = true */ public void demo2() { Session session = HibernateUtils.getCurrentSession(); Transaction transaction = session.beginTransaction(); // 查询1号客户 Customer customer = session.get(Customer.class, 1l);// 发送一条查询客户的sql System.out.println(customer.getCust_name()); // 查看1号客户每个联系人的信息 for (LinkMan linkMan : customer.getLinkMans()) {// 发送一条根据客户查询联系人的sql System.out.println(linkMan.getLkm_name()); } transaction.commit(); } @Test /** * 设置fetch=select,lazy =false */ public void demo3() { Session session = HibernateUtils.getCurrentSession(); Transaction transaction = session.beginTransaction(); // 查询1号客户 Customer customer = session.get(Customer.class, 1l);// 不延迟,直接发送两条语句,查询客户的sql,根据客户查询联系人的sql System.out.println(customer.getCust_name()); System.out.println(customer.getLinkMans().size());// 发送一条select // count(lkm_id) // from transaction.commit(); } @Test /** * 设置fetch=select,lazy =extra */ public void demo4() { Session session = HibernateUtils.getCurrentSession(); Transaction transaction = session.beginTransaction(); // 查询1号客户 Customer customer = session.get(Customer.class, 1l);// 发送一条查询客户语句 System.out.println(customer.getCust_name()); System.out.println(customer.getLinkMans().size());// 发送一条select // count(lkm_id) // from transaction.commit(); } @Test /** * 设置fetch=join,lazy =失效 */ public void demo5() { Session session = HibernateUtils.getCurrentSession(); Transaction transaction = session.beginTransaction(); // 查询1号客户 Customer customer = session.get(Customer.class, 1l);// 发送一条迫切左外连接语句,两表全查 System.out.println(customer.getCust_name()); System.out.println(customer.getLinkMans().size());// 不发语句 transaction.commit(); } @Test /** * 设置fetch=subselect,lazy =true */ public void demo6() { Session session = HibernateUtils.getCurrentSession(); Transaction transaction = session.beginTransaction(); // 查询1号客户 List<Customer> list = session.createQuery("from Customer").list();// 发送查询所有客户的sql语句 for (Customer customer : list) { System.out.println(customer.getCust_name()); System.out.println(customer.getLinkMans().size());// 发送一条子查询 } transaction.commit(); } @Test /** * 设置fetch=subselect,lazy =false */ public void demo7() { Session session = HibernateUtils.getCurrentSession(); Transaction transaction = session.beginTransaction(); // 查询1号客户 List<Customer> list = session.createQuery("from Customer").list();// 发送查询所有客户的sql语句,发送一条子查询 for (Customer customer : list) { System.out.println(customer.getCust_name()); System.out.println(customer.getLinkMans().size()); } transaction.commit(); } }
fetch:抓取策略
lazy:延迟加载,控制查询关联对象的时候是否采用延迟
一般使用默认值
各种配置的区别测试:
/** * 在<many-to-one>上的fetch和lazy */ public class HibernateDemo5 { @Test /** * 默认情况 */ public void demo1() { Session session = HibernateUtils.getCurrentSession(); Transaction transaction = session.beginTransaction(); LinkMan linkMan = session.get(LinkMan.class, 1l);// 发送一条查询联系人的语句 System.out.println(linkMan.getLkm_name()); System.out.println(linkMan.getCustomer());// 发送一条select查询联系人所关联的客户的语句 transaction.commit(); } @Test /** * fetch = "select" lazy = "proxy",默认值 */ public void demo2() { Session session = HibernateUtils.getCurrentSession(); Transaction transaction = session.beginTransaction(); LinkMan linkMan = session.get(LinkMan.class, 1l);// 发送一条查询联系人的语句 System.out.println(linkMan.getLkm_name()); System.out.println(linkMan.getCustomer());// 发送一条select查询联系人所关联的客户的语句 transaction.commit(); } @Test /** * fetch = "select" lazy = "false" */ public void demo3() { Session session = HibernateUtils.getCurrentSession(); Transaction transaction = session.beginTransaction(); LinkMan linkMan = session.get(LinkMan.class, 1l);// 不延迟,发送一条查询联系人的语句,发送一条select查询联系人所关联的客户的语句 System.out.println(linkMan.getLkm_name()); System.out.println(linkMan.getCustomer()); transaction.commit(); } @Test /** * fetch = "join" lazy = 失效 */ public void demo4() { Session session = HibernateUtils.getCurrentSession(); Transaction transaction = session.beginTransaction(); LinkMan linkMan = session.get(LinkMan.class, 1l);// 发送一条迫切左外连接,两表全查 System.out.println(linkMan.getLkm_name()); System.out.println(linkMan.getCustomer()); transaction.commit(); } }
批量抓取:一批关联对象一起抓取,batch-size,
/** * Hibernate批量抓取 */ public class HibernateDemo6 { @Test /** * 获取客户的时候批量抓取联系人,在Customer.hbm.xml中设置set标签batch-size="4" */ public void demo1() { Session session = HibernateUtils.getCurrentSession(); Transaction transaction = session.beginTransaction(); List<Customer> list = session.createQuery("from Customer").list(); for (Customer customer : list) { System.out.println(customer.getCust_name()); for (LinkMan linkMan : customer.getLinkMans()) { System.out.println(linkMan.getLkm_name()); } } transaction.commit(); } @Test /** * 获取联系的时候批量抓取客户,在Customer.hbm.xml中设置class标签batch-size="4" */ public void demo2() { Session session = HibernateUtils.getCurrentSession(); Transaction transaction = session.beginTransaction(); List<LinkMan> list = session.createQuery("from LinkMan").list(); for (LinkMan linkMan : list) { System.out.println(linkMan.getLkm_name()); System.out.println(linkMan.getCustomer().getCust_name()); } transaction.commit(); } }
标签:select查询 col ESS none 方法 加载 action proxy alt
原文地址:https://www.cnblogs.com/ltfxy/p/10313664.html