Auto-populated new entity collection disappearing on edit

Greetings,

I’m having an issue auto-populating composition records and getting them to stay on the screen when editing any of them. I’ve created a basic project to show my issues.

Please assume 2 entities:

Order

  • order String
  • milestones Composition

Milestone

  • milestone String

The OrderEdit controller looks like this:

public class OrderEdit extends StandardEditor<Order> {
    @Inject
    private DataContext dataContext;
    private List<Milestone> milestones = new ArrayList<>();

    @Subscribe
    public void onInitEntity(InitEntityEvent<Order> event) {
        event.getEntity().setMilestones(getDefaultMilestones());
    }

    private List<Milestone> getDefaultMilestones() {
        createMilestone("Milestone 3");
        createMilestone("Milestone 5");
        return milestones;
    }

    private void createMilestone(String milestoneText) {
        Milestone milestone = dataContext.create(Milestone.class);
        milestone.setOrder(this.getEditedEntity());
        milestone.setMilestone(milestoneText);
        milestones.add(milestone);
    }
}

These items populate correctly in the composition table on the Order editor when creating a new Order, however, when editing any of these collection entity records and returning to the Order editor, nothing appears in the Milestone collection table. Note, that these entities do save correctly when saving the Order and coming back, but the problem is that they disappear before saving.

Here is an example project.
collection.zip (93.0 KB)

Thanks in advance for your help,
Adam

I had a very, very similar problem a while back and can’t remember the solution. I’m looking through my post history trying to find it (because I definitely posted about it!) but I can’t find the info yet. O_O

Something to do with having to tell the table that the collection was changed.

Aha - in my case I was creating the new entities into the DataContainer itself via getMutableItems.add(...) - and the programmatically created entities weren’t merged to the DataContext. You’re doing it via creating the entities in the parent entity’s collection, etc - which should be totally valid!

Though you might try the approach of creating them and adding them to the DataContainer and letting it go that way instead. That definitely works.

Code snippet for doing it that way:

               for (MeasurementType measurementType : measurementTypes) {
                    PatientMeasurement pm = dataManager.create(PatientMeasurement.class);
                    pm.setPatient(getEditedEntity());
                    pm.setDate(new Date());
                    pm.setDeviceType(deviceType);
                    pm.setMeasurementType(measurementType);
                    pm = dataContext.merge(pm);
                    measurementsDc.getMutableItems().add(pm);
                }

Lifted directly from my code, but you should be able to adapt from that.

Hey Jon,

I really appreciate the help. I wasn’t quite able to get the example working (I suspect because I was calling the methods from onInitEntity() and not everything was set up yet), so I moved my call to the method into onAfterShow() and injected EntityStates (which I’ve never used before).

@Subscribe
public void onAfterShow(AfterShowEvent event) {
    if (entityStates.isNew(getEditedEntity)) {
        createDefaultMilestones();
    }
}

Those steps, along with either of our previous code gets it functioning properly. All is well in the world!!!

Thanks again for taking the time to help,
Adam

1 Like