Trying to confirm lazy loading is working

2019-07-22 11:34发布

I am learning hibernate and i am using the debugger to understand how hibernate fetches objects. I have enabled lazy loading in my XML and i use create a query to get an object. This object has a Set associated with it.

When i look at the variable values in my debugger it seems to be completely loading the object graph, ie not lazily fetching the set and its properties. I can think of several reasons why this might be the case.

1) something is wrong with my xml / config and i have eager fetching enabled. I'm not sure this is the case, everything is at its defaults which is to lazy fetch.

2) The way the IDE debugger works is that is causes hibernate to trigger the lazy loading when i look at them.

3) Hibernate is fetching the entire object graph from the 2nd level cache. Ive tried to disable this but this seems to have no effect

Any other ideas?

2条回答
唯我独甜
2楼-- · 2019-07-22 12:25

@atamanroman's answer is fully correct. I just wanted to add in some details. In order to test lazy loading in a JavaEE environment, I use Arquillian tests. In the test class, use a UserTransaction to control transaction boundaries. After committing the transaction, access the lazy loaded attribute and expect an Exception

@Inject
private UserTransaction tx;

@Test(expected=Exception.class)
public void testLazyLoading() throws Exception {

    tx.begin();
    MyEntity entity = em.find(entityId);
    tx.commit();

    entity.getLazyLoadedProperty().someMethod();
} 

Please note that in order to trigger the loading of a lazy loaded attribute, it is often not enough to call the getter inside the transaction, you must also perform some operation with the attribute, like calling size() on a list. Otherwise, the getter will simply return a proxy.

查看更多
Ridiculous、
3楼-- · 2019-07-22 12:29

Debugging (showing the lazy fields, to be precise) triggers the lazy loading already. Get the entity, detach it and access your lazy field. Should throw some sort of exception if lazy loading works.

If you really want to know how hibernate does it, have a look at the sources.

查看更多
登录 后发表回答