標籤:round demo 代碼 close 客戶 public action batch 檢索
立即檢索:當執行某行代碼的時候,馬上發出SQL語句進行查詢(get())
延遲檢索:當執行某行代碼的時候,不會馬上發出SQL語句進行查詢.當真正使用這個對象的時候才會發送SQL語句(load())
類層級檢索和關聯層級檢索
類層級的檢索:<class>標籤上配置lazy
關聯層級的檢索:<set>/<many-to-one>上面的lazy
從一的一方關聯多的一方(<set>)
fetch:控制sql語句的類型
join:發送迫切左外串連的SQL查詢關聯對象.fetch=”join”那麼lazy被忽略了.
select:預設值,發送多條SQL查詢關聯對象.
subselect:發送子查詢查詢關聯對象.(需要使用Query介面測試)
lazy:控制關聯對象的檢索是否採用延遲.
true:預設值, 查詢關聯對象的時候使用延遲檢索
false:查詢關聯對象的時候不使用延遲檢索.
extra:及其懶惰.
***** 如果fetch是join的情況,lazy屬性將會忽略
在多的一方關聯一的一方(<many-to-one>)
fetch:控制SQL語句發送格式
join:發送一個迫切左外串連查詢關聯對象.fetch=”join”,lay屬性會被忽略.
select:發送多條SQL檢索關聯對象.
lazy:關聯對象檢索的時候,是否採用延遲
false:不延遲
proxy:使用代理.檢索訂單額時候,是否馬上檢索客戶 由Customer對象的對應檔中<class>上lazy屬性來決定.
no-proxy:不使用代理(不研究)
執行個體:
/* * 區分立即檢索和延遲檢索 */// 立即檢索Customer customer = (Customer) session.get(Customer.class, 1);System.out.println(customer); // 延遲檢索:// 持久化類如果設定為final 延遲檢索就失效了.// 在Customer.hbm.xml中在<class>標籤上配置lazy="false"不支援延遲檢索,就會立即檢索.Customer customer = (Customer) session.load(Customer.class, 1);System.out.println(customer);
/* * <set>沒有配置fetch 和 lazy情況 */public void demo2(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction();
Customer customer = (Customer) session.get(Customer.class, 1);// 發送查詢客戶的SQL. System.out.println(customer.getOrders().size());// 又發送一條SQL 去查詢客戶的關聯的訂單 tx.commit(); session.close();
}
@Test/* * <set>配置fetch="join" lazy就會被忽略!!! * * 直接發送迫切左外串連查詢兩個表. */public void demo3(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); // 直接發送一條迫切左外串連 /* * select customer0_.cid as cid0_1_, customer0_.cname as cname0_1_, orders1_.cno as cno0_3_, orders1_.oid as oid3_, orders1_.oid as oid1_0_, orders1_.addr as addr1_0_, orders1_.cno as cno1_0_ from customer customer0_ left outer join orders orders1_ on customer0_.cid=orders1_.cno where customer0_.cid=? */ Customer customer = (Customer) session.get(Customer.class, 1); System.out.println(customer.getOrders().size()); tx.commit(); session.close();}
@Test /* * 在<set>集合上配置 * * fetch="select" lazy="true" * * lazy:true-使用延遲檢索 * * 發送多條SQL,查詢關聯對象 */ public void demo4(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); // 發送一條只查詢客戶的SQL Customer customer = (Customer) session.get(Customer.class, 1); // 使用訂單的時候又發送一條查詢這個客戶的訂單的SQL System.out.println(customer.getOrders().size()); tx.commit(); session.close(); }
@Test /* * 在<set>集合上配置 * * fetch="select" lazy="false" * * lazy:false:關聯對象的檢索不使用延遲 */ public void demo5(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); // 發送多條SQL,查詢關聯對象. Customer customer = (Customer) session.get(Customer.class, 1); System.out.println(customer.getOrders().size()); tx.commit(); session.close(); }
@Test /* * 在<set>集合上配置 * * fetch="select" lazy="extra" * * lazy:extra極其懶惰.要訂單的數量 */ public void demo6(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); Customer customer = (Customer) session.get(Customer.class, 1); // hibernate發送select count(*) from orders where cno = ?; System.out.println(customer.getOrders().size()); // 發送查詢訂單的SQL for (Order order : customer.getOrders()) { System.out.println(order); } tx.commit(); session.close(); }
@Test /* * 在<set>集合上配置 * * fetch="subselect" lazy="true" * * 使用subselect的時候 需要使用 query介面進行測試. * * 查詢一個客戶 查詢多個客戶. * 如果有多個客戶: * * select * from orders where cno in (1,2,3); * 如果只有一個客戶: * * select * from orders where cno = 1; */ public void demo7(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); List<Customer> list = session.createQuery("from Customer").list(); for (Customer customer : list) { System.out.println(customer.getOrders().size()); } tx.commit(); session.close(); }
/* * 沒有在<many-to-one>標籤上配置: * * 發送多條SQL進行查詢. */ public void demo8(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); // 只會發送一條查詢訂單SQL. Order order = (Order) session.get(Order.class, 1); // 使用訂單的客戶對象的時候,又發送一條SQL查詢訂單關聯的客戶 System.out.println(order.getCustomer().getCname()); tx.commit(); session.close(); }
/* * 在<many-to-one>標籤上配置: * * fetch="join" lazy="被忽略" * * 發送迫切左外串連 */ public void demo9(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); // 發送一條迫切左外串連.查詢關聯對象. Order order = (Order) session.get(Order.class, 1); System.out.println(order.getCustomer().getCname()); tx.commit(); session.close(); }
/* * 在<many-to-one>標籤上配置: * * fetch="select" lazy="false" * * 發送多條SQL */ public void demo10(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); // 在這行發送多條SQL 查詢關聯對象. Order order = (Order) session.get(Order.class, 1); System.out.println(order.getCustomer().getCname()); tx.commit(); session.close(); }
/* * 批量抓取 * 在客戶一端配置 * <set>集合上配置batch-size="3" */ public void demo12(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); List<Customer> list = session.createQuery("from Customer").list(); for (Customer customer : list) { for (Order order : customer.getOrders()) { System.out.println(order.getAddr()); } } tx.commit(); session.close(); }
/* * 批量抓取 * 通過訂單批量抓取客戶: * 需要在客戶一端<class>標籤上配置batch-size */ public void demo13(){ Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); List<Order> list = session.createQuery("from Order").list(); for (Order order : list) { System.out.println(order.getCustomer().getCname()); } tx.commit(); session.close(); }
Hibernate的抓取策略