Lazy loading bug with composition

Steps to reproduce:

  • create a parent and child entity with composition one-to-many mapping
  • create browse/edit screens
  • remove fetch plan from edit screen (see commented out xml in test project)
  • create a parent entity with a child
  • save, open again to edit, edit child’s data (attribute “name” in test project) and try to save again

It won’t work, because the parentEntity attribute in the child will be null.

Minimum test project: compositionbug.zip (88.3 KB)

Thank you for the test project.

The problem here is twofold.

First, you cannot rely on lazy loading on screens with nested containers. It’s because nested instances loaded lazily will bypass DataContext and won’t be tracked for changes. So a fetch plan including nested collection is required.

Second, thanks to your use case, I discovered an issue with Kotlin entities and lazy loading of collections:

It causes the empty back reference to Parent. If you change the field declaration to nullable, the lazy loading will work (but the first problem will prevent changes to be saved automatically):

@Composition
@OneToMany(mappedBy = "parentEntity")
var children: MutableList<ChildEntity>? = null
1 Like