Mybatis-3: aggressiveLazyLoading doesn't work as document says

Created on 5 Nov 2015  ·  11Comments  ·  Source: mybatis/mybatis-3

document says about aggressiveLazyLoading:

When enabled, an object with lazy loaded properties will be loaded entirely upon a call to any of the lazy properties. Otherwise, each property is loaded on demand.

But the actual result is when I call method on an non-lazy propertiy, the lazy load is also triggered.

See also this stackoverflow question: http://stackoverflow.com/questions/24013855/lazy-loading-using-mybatis-3-with-java

bug documentation

All 11 comments

So, it's a documentation issue, right?
Is the following explanation correct?

When enabled, a call to one lazy property will load all the lazy properties of the object. See also lazyLoadTriggerMethods.

Sorry, the explanation was inaccurate.

When enabled, any method call will load all the lazy properties of the object.

I think it's a documentation bug. Hence; we should be modify a explanation in reference document.

When enabled, any method call will load all the lazy properties of the object.

👍

I have updated the doc and added a test case.

While writing the test, I realized that aggressiveLazyLoading is more aggressive than I thought.

With a result map like below, for example, I expected that the lazy query getLazyProp is executed when some method is invoked from the user's code, but it actually is executed when MyBatis invokes the setter method of id.
So, in some (most?) use cases, aggressiveLazyLoading works almost the same way as eager loading, it seems.
This behavior has not changed since 3.1.0, at least.

  <resultMap type="test.User" id="user">
    <id property="id" column="id" />
    <result property="name" column="name" />
    <association property="lazyProp" column="id" select="getLazyProp" />
  </resultMap>

@christianpoitras
As you've done some research on lazy loading before, please let me know if I am missing something!

You are probably right. With aggressive lazy loading, the properties are loaded as soon as any method is called in the class.
So if MyBatis calls a setter and does not disable the lazy load temporarily, all lazy properties will loaded.

I cannot confirm if versions before 3.1.0 were working differently back then. I remember that lazy load was quite aggressive, but I think it did not load all the lazy properties.

Disabling lazy loading while MyBatis is setting known properties is feasible, but I don't think many people is using aggressive lazy load so it may not be worth the time.

Thanks for the comment, @christianpoitras !

Disabling lazy loading while MyBatis is setting known properties is feasible, but I don't think many people is using aggressive lazy load so it may not be worth the time.

Agreed.
Reading your comment, false seems more appropriate for the default value of aggressiveLazyLoading, but I'll leave it as it is for now.

BTW, I used this to test older versions' behavior.

It is reasonable to change the default setting in a minor version.

I though the default value was already set to false for aggressive lazy load...

Oh, OK. I'll change it later along with the docs, then.
Thanks for the input!

I agree to change the default value to false.

Thanks for the upvote, @kazuki43zoo !

Thanks!

Was this page helpful?
0 / 5 - 0 ratings