2012-01-11 90 views
0

我有一些休眠查询,我期望一些对象被快速加载,而另一些则是延迟加载。 我目前正在检查急切加载的方法是测量访问属性所用的时间。如何以编程方式检查懒惰/渴望加载

//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. 

这个问题很有缺陷,因为测试会因系统性能而失败。有没有更好的办法?

回答

3

我会用

Hibernate.isInitialized(customer.getProducts())