How to avoid the "During sync ... new object .. not marked cascade PERSIST" when doing purely programmatic data importing?

So I know that when doing programmatic data creation from a SCREEN, when one gets the “During synchronization a new object was found through a relationship that was not marked cascade PERSIST” error, that means one needs to add the objects to the DataContext.

What happens when one is doing purely programmatic data creation not involving a screen at all, i.e., from a service, and this error happens?

For example I have Patient entities that have children Address entities. For performance, I’m using a SaveContext with setDiscardSaved(true). I create the Patient, add it to the SaveContext, then create it’s Address children and add those to the SC.

When I call dataManager.save(saveContext) - it tries to save the addresses first, thus leading to the above error.

Is there a way around this, while still getting the insanely high performance of 'setDiscardSaved(true)?

Hi Jon,

setDiscardSaved(true) doesn’t affect saving of linked entities in one go.
Something like this should work:

  Order order = dataManager.create(Order.class);
  order.setDate(LocalDate.now());

  OrderLine orderLine = dataManager.create(OrderLine.class);
  orderLine.setOrder(order);
  orderLine.setName("foo");
  orderLine.setAmount(BigDecimal.TEN);

  SaveContext saveContext = new SaveContext().saving(order, orderLine).setDiscardSaved(true);

  dataManager.save(saveContext);

Please provide more information on your use case.