Possibilities of @OneToMany in EntitySavingEvent

I’m wondering what’s the rule related to @OneToMany fields when received in EntitySavingEvent? Like in what situation will I get what, because otherwise things turn wired.

My JMix version is 2.3.0, I’m both manipulating the entity on webpage, having API access to entity using REST plugin, and saving another entity in one entity. Currently I’ve found following situations for @OneToMany List<...> fields provided by EntitySavingEvent.getEntity():

  • null
  • IndirectList: a list, but will error on any operation (e.g. isEmpty()) on it
  • an empty list: does this mean it’s actually empty or it’s not initialized?
  • a regular list: does this mean content inside is fully initialized, or they can also be: null / SomeIndirectObject? In case of indirect object, can their id be accessed through Id.of(...) / getId(), or those ids are not accessible through simple methods?

Since all kinds of error can happen, now I always try to dataManager.load related entity before trying to access something inside, but there are situation where the entity is not saved yet or the incoming entity contains update, so the logic become really messy.

Any systematic way of dealing with these situations? Thanks!

Unfortunately the framework cannot guarantee the presence of the referenced instances in EntitySavingEvent listener and cascading of events when references are loaded from an EntityLoadingEvent listener.

So we don’t recommend accessing references in these listeners. Only the local state should be managed.

For working with related entities, use the EntityChangedEvent.

Regards,
Konstantin