Session: How to Defer Operations

Operations can be deferred till saveChanges is called by using defer method in advanced session operations. All of the operations will update session state appropriately after saveChanges is called.

Types of commands that can be deferred:

Syntax

void defer(ICommandData command, ICommandData... commands);

void defer(ICommandData[] commands);
Parameters
ICommandData Command to be executed.  
ICommandData[] Array of commands implementing ICommandData interface.  

Example

Map<String, Object> value1 = new HashMap<>();
value1.put("Name", "My Product");
value1.put("Supplier", "suppliers/999-A");
value1.put("@metadata", Collections.singletonMap("@collection", "Users"));

PutCommandDataWithJson putCommand1 =
    new PutCommandDataWithJson("products/999-A",
        null,
        store.getConventions().getEntityMapper().valueToTree(value1));

HashMap<String, Object> value2 = new HashMap<>();
value2.put("Name", "My Product");
value2.put("Supplier", "suppliers/999-A");
value2.put("@metadata", Collections.singletonMap("@collection", "Suppliers"));

PutCommandDataWithJson putCommand2 =
    new PutCommandDataWithJson("suppliers/999-A",
        null,
        store.getConventions().getEntityMapper().valueToTree(value2));

DeleteCommandData command3 = new DeleteCommandData("products/1-A", null);

session.advanced().defer(putCommand1, putCommand2, command3);