Binding of lazy loaded form property

I have a class that extends FormLayout and includes several fields which are bound to the jpa entity via binder.bindInstanceFields(myForm) The binder instance is of type Gewaesser.class. Theres a field “fischarten” of type MultiSelectComboBox inside myForm. When setting the fetch type for the field fischarten inside the entity to EAGER the binder works just fine. Now I figured out that every time I load a Gewaesser-instance from the DB, hibernate performs an additional query for every to get the fischarten (List) even if I dont need them. After removing EAGER fetching for fischarten field from Gewaesser entity the binder will no longer work: I’m getting the well known error 'org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: x.y.persistence.entity.Gewaesser.fischarten: could not initialize proxy - no Session'

How can I fix this without setting the fetch type to eager?

I was able to find away around the error. Before reading the Bean I fetch the List first and populate gewaesser.fischarten with the list.

this.gewaesser = getGewaesser();
this.gewaesser.setFischarten(fischartService.findByGewaesserId(this.gewaesser.getId()));

binder.readBean(this.gewaesser);

But now I have another problem… When changing the selection of fischarten via the MultiSelectCombobox and trigger the save-action the new fischarten-selection will not be persisted to the db.

I guess this can be solved via cascade?

I found the issue. The native query behind fischartService.findByGewaesserId were wrong. Now it works :slightly_smiling_face: