Hibernate / MyBatis Interview questions
What is lazy loading in MyBatis, and how do you configure it?
Lazy loading defers fetching an associated object or collection until the moment it's actually accessed in application code, rather than fetching it eagerly as part of the initial query, which can avoid unnecessary work when a related object isn't always needed.
<settings> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> </settings>
Lazy loading is enabled globally via the lazyLoadingEnabled setting in mybatis-config.xml (or the equivalent Spring Boot property), and it applies specifically to nested-select-based associations and collections — when enabled, MyBatis returns a proxy object for the lazily-loaded property, and the actual nested-select query only fires the first time that property is genuinely accessed.
The aggressiveLazyLoading setting controls a related nuance: when enabled (the historical default in older MyBatis versions), accessing any lazy property on an object triggers loading of all its lazy properties at once; when disabled (the modern recommended default), each lazy property loads independently, only when that specific property is accessed, which is generally the more predictable and efficient behavior for most applications.
More Related questions...