我有一些hibernate查询,我希望有些对象可以被永久加载,而其他对象则是延迟加载的。 我当前用于检查预先加载的approch是衡量访问房产所需的时间。
//Call the method i want to test.
Customer customer = loadCustomerWithProducts();
long start = System.currentTimeMillis();
//Call a method on an object that sould have been eagerly loaded.
customer.getProducts().get(0).getName();
long end = System.currentTimeMillis();
long diff = end - start;
Assert.assertTrue(diff < 50); //Assuming that hitting the database takes longer than 50ms.
这种方法存在很大缺陷,因为测试会因系统性能而失败。还有更好的方法吗?
答案 0 :(得分:3)
我会用
Hibernate.isInitialized(customer.getProducts())
答案 1 :(得分:0)
有两种方法:
Hibernate.inInitialized(customer.getProducts())
。