输出: Hibernate: select customer0_.hb_customer_id as hb1_2_0_, customer0_.hb_customer_name as hb2_2_0_ from mage.hb_customer customer0_ where customer0_.hb_customer_id=? class com.mamh.hibernate.demo.entities.Customer =destroy=
延迟加载: 如果不访问任何order对象是不会取加载它的。 Hibernate: select customer0_.hb_customer_id as hb1_2_0_, customer0_.hb_customer_name as hb2_2_0_ from mage.hb_customer customer0_ where customer0_.hb_customer_id=? aa
延迟加载, 如果 有这样的 调用 customer中的集合order属性的话就会加载oder对象。 System.out.println(customer.getOrders()); Hibernate: select customer0_.hb_customer_id as hb1_2_0_, customer0_.hb_customer_name as hb2_2_0_ from mage.hb_customer customer0_ where customer0_.hb_customer_id=? aa Hibernate: select orders0_.customer_id as customer3_2_1_, orders0_.hb_order_id as hb1_1_, orders0_.hb_order_id as hb1_3_0_, orders0_.hb_order_name as hb2_3_0_, orders0_.customer_id as customer3_3_0_ from mage.hb_order orders0_ where orders0_.customer_id=? order by orders0_.hb_order_id [Order{orderId=1, orderName='aa', customer=Customer{customerId=1, customerName='aa'}} , Order{orderId=2, orderName='bb', customer=Customer{customerId=1, customerName='aa'}} , Order{orderId=3, orderName='cc', customer=Customer{customerId=1, customerName='aa'}} ]
这里lazy="false"就表示立即加载了。 Hibernate: select customer0_.hb_customer_id as hb1_2_0_, customer0_.hb_customer_name as hb2_2_0_ from mage.hb_customer customer0_ where customer0_.hb_customer_id=? Hibernate: select orders0_.customer_id as customer3_2_1_, orders0_.hb_order_id as hb1_1_, orders0_.hb_order_id as hb1_3_0_, orders0_.hb_order_name as hb2_3_0_, orders0_.customer_id as customer3_3_0_ from mage.hb_order orders0_ where orders0_.customer_id=? order by orders0_.hb_order_id aa
Hibernate: select customer0_.hb_customer_id as hb1_2_0_, customer0_.hb_customer_name as hb2_2_0_ from mage.hb_customer customer0_ where customer0_.hb_customer_id=? aa Hibernate: select count(hb_order_id) from mage.hb_order where customer_id =? 3
直接手动调用初始化集合对象
1
Hibernate.initialize(customer.getOrders());
大部分取默认值,lazy= “true”
set元素的batch-size属性
这个可以设定一次初始化集合的数量。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
@Test public void testBatchSize(){ List<Customer> customers = session.createQuery("from Customer ").list();
System.out.println(customers.size());
}
Hibernate: select customer0_.hb_customer_id as hb1_2_, customer0_.hb_customer_name as hb2_2_ from mage.hb_customer customer0_ 4
System.out.println(customers.size()); for (Customer customer : customers) { if (customer.getOrders() != null) { System.out.println(customer.getOrders().size()); } }
}
Hibernate: select customer0_.hb_customer_id as hb1_2_, customer0_.hb_customer_name as hb2_2_ from mage.hb_customer customer0_ 4
Hibernate: select orders0_.customer_id as customer3_2_1_, orders0_.hb_order_id as hb1_1_, orders0_.hb_order_id as hb1_3_0_, orders0_.hb_order_name as hb2_3_0_, orders0_.customer_id as customer3_3_0_ from mage.hb_order orders0_ where orders0_.customer_id=? order by orders0_.hb_order_id 3 Hibernate: select orders0_.customer_id as customer3_2_1_, orders0_.hb_order_id as hb1_1_, orders0_.hb_order_id as hb1_3_0_, orders0_.hb_order_name as hb2_3_0_, orders0_.customer_id as customer3_3_0_ from mage.hb_order orders0_ where orders0_.customer_id=? order by orders0_.hb_order_id 3 Hibernate: select orders0_.customer_id as customer3_2_1_, orders0_.hb_order_id as hb1_1_, orders0_.hb_order_id as hb1_3_0_, orders0_.hb_order_name as hb2_3_0_, orders0_.customer_id as customer3_3_0_ from mage.hb_order orders0_ where orders0_.customer_id=? order by orders0_.hb_order_id 3
Hibernate: select orders0_.customer_id as customer3_2_1_, orders0_.hb_order_id as hb1_1_, orders0_.hb_order_id as hb1_3_0_, orders0_.hb_order_name as hb2_3_0_, orders0_.customer_id as customer3_3_0_ from mage.hb_order orders0_ where orders0_.customer_id=? order by orders0_.hb_order_id 0
Hibernate: select customer0_.hb_customer_id as hb1_2_, customer0_.hb_customer_name as hb2_2_ from mage.hb_customer customer0_ 4 Hibernate: select orders0_.customer_id as customer3_2_1_, orders0_.hb_order_id as hb1_1_, orders0_.hb_order_id as hb1_3_0_, orders0_.hb_order_name as hb2_3_0_, orders0_.customer_id as customer3_3_0_ from mage.hb_order orders0_ where orders0_.customer_id in ( ?, ?, ?, ? ) order by orders0_.hb_order_id 3 3 3 0
Hibernate: select customer0_.hb_customer_id as hb1_2_, customer0_.hb_customer_name as hb2_2_ from mage.hb_customer customer0_ 4 Hibernate: select orders0_.customer_id as customer3_2_1_, orders0_.hb_order_id as hb1_1_, orders0_.hb_order_id as hb1_3_0_, orders0_.hb_order_name as hb2_3_0_, orders0_.customer_id as customer3_3_0_ from mage.hb_order orders0_ where orders0_.customer_id in ( ?, ? ) order by orders0_.hb_order_id 3 3 Hibernate: select orders0_.customer_id as customer3_2_1_, orders0_.hb_order_id as hb1_1_, orders0_.hb_order_id as hb1_3_0_, orders0_.hb_order_name as hb2_3_0_, orders0_.customer_id as customer3_3_0_ from mage.hb_order orders0_ where orders0_.customer_id in ( ?, ? ) order by orders0_.hb_order_id 3 0
Hibernate: select customer0_.hb_customer_id as hb1_2_, customer0_.hb_customer_name as hb2_2_ from mage.hb_customer customer0_ 4 Hibernate: select orders0_.customer_id as customer3_2_1_, orders0_.hb_order_id as hb1_1_, orders0_.hb_order_id as hb1_3_0_, orders0_.hb_order_name as hb2_3_0_, orders0_.customer_id as customer3_3_0_ from mage.hb_order orders0_ where orders0_.customer_id in ( ?, ? ) order by orders0_.hb_order_id 3 3 Hibernate: select orders0_.customer_id as customer3_2_1_, orders0_.hb_order_id as hb1_1_, orders0_.hb_order_id as hb1_3_0_, orders0_.hb_order_name as hb2_3_0_, orders0_.customer_id as customer3_3_0_ from mage.hb_order orders0_ where orders0_.customer_id in ( ?, ? ) order by orders0_.hb_order_id 3 0
Hibernate: select customer0_.hb_customer_id as hb1_2_, customer0_.hb_customer_name as hb2_2_ from mage.hb_customer customer0_ 4 Hibernate: select orders0_.customer_id as customer3_2_1_, orders0_.hb_order_id as hb1_1_, orders0_.hb_order_id as hb1_3_0_, orders0_.hb_order_name as hb2_3_0_, orders0_.customer_id as customer3_3_0_ from mage.hb_order orders0_ where orders0_.customer_id in ( select customer0_.hb_customer_id from mage.hb_customer customer0_ ) order by orders0_.hb_order_id 3 3 3 0
Hibernate: select customer0_.hb_customer_id as hb1_2_, customer0_.hb_customer_name as hb2_2_ from mage.hb_customer customer0_ 4 Hibernate: select orders0_.customer_id as customer3_2_1_, orders0_.hb_order_id as hb1_1_, orders0_.hb_order_id as hb1_3_0_, orders0_.hb_order_name as hb2_3_0_, orders0_.customer_id as customer3_3_0_ from mage.hb_order orders0_ where orders0_.customer_id in ( ?, ? ) order by orders0_.hb_order_id 3 3 Hibernate: select orders0_.customer_id as customer3_2_1_, orders0_.hb_order_id as hb1_1_, orders0_.hb_order_id as hb1_3_0_, orders0_.hb_order_name as hb2_3_0_, orders0_.customer_id as customer3_3_0_ from mage.hb_order orders0_ where orders0_.customer_id in ( ?, ? ) order by orders0_.hb_order_id 3 0
Hibernate: select customer0_.hb_customer_id as hb1_2_1_, customer0_.hb_customer_name as hb2_2_1_, orders1_.customer_id as customer3_2_3_, orders1_.hb_order_id as hb1_3_, orders1_.hb_order_id as hb1_3_0_, orders1_.hb_order_name as hb2_3_0_, orders1_.customer_id as customer3_3_0_ from mage.hb_customer customer0_ left outer join mage.hb_order orders1_ on customer0_.hb_customer_id=orders1_.customer_id where customer0_.hb_customer_id=? order by orders1_.hb_order_id 3
@Test public void testManyToOneGet() { Order order = (Order) session.get(Order.class, 1); System.out.println(order.getOrderName() + ": " + order.getOrderId());
//Customer customer = order.getCustomer(); //System.out.println(customer); } Hibernate: select order0_.hb_order_id as hb1_3_0_, order0_.hb_order_name as hb2_3_0_, order0_.customer_id as customer3_3_0_ from mage.hb_order order0_ where order0_.hb_order_id=? aa: 1
Hibernate: select order0_.hb_order_id as hb1_3_0_, order0_.hb_order_name as hb2_3_0_, order0_.customer_id as customer3_3_0_ from mage.hb_order order0_ where order0_.hb_order_id=? Hibernate: select customer0_.hb_customer_id as hb1_2_0_, customer0_.hb_customer_name as hb2_2_0_ from mage.hb_customer customer0_ where customer0_.hb_customer_id=? aa: 1
Hibernate: select order0_.hb_order_id as hb1_3_1_, order0_.hb_order_name as hb2_3_1_, order0_.customer_id as customer3_3_1_, customer1_.hb_customer_id as hb1_2_0_, customer1_.hb_customer_name as hb2_2_0_ from mage.hb_order order0_ left outer join mage.hb_customer customer1_ on order0_.customer_id=customer1_.hb_customer_id where order0_.hb_order_id=? aa: 1