Upsert multiple objects¶
Description¶
When performing this operation, the system attempts to locate and update a specific set of objects first. If some or all of these objects cannot be found, any objects specified in the operation's invocation are inserted into the data table, while any remaining objects are simply updated. This process is based on using the unique identifiers (objectId
) of the records to apply any necessary changes in the data table.
Method¶
<E> OpResult bulkUpsert( List<E> instances );
OpResult bulkUpsert( String tableName, List<Map<String, Object>> arrayOfObjectMaps );
where:
Argument | Description |
---|---|
tableName |
Name of the table where objects will be upserted. |
instances |
Identifies an array of class objects that must be upserted in the data table. |
arrayOfObjectMaps |
Identifies an array of objects that must be upserted in the data table. |
The difference between the two signatures is in the type of input parameters they accept and the way they are used.
The first signature expects an array of object instances that represents the data you want to upsert into the Backendless database. This means you can pass an array of objects of any class that correspond to tables in the database. The objects should have the necessary properties and values that need to be upserted. The method will perform the bulk upsert operation using the provided objects and return an OpResult object that contains the result of the operation.
The second signature accepts the name of the table (tableName
) and an array of Map objects (arrayOfObjectMaps
) that contain the column names and their corresponding values. This provides a more flexible way to specify the data to be upserted without requiring specific object instances. The arrayOfObjectMaps
should have key-value pairs where the key represents the column name and the value represents the value to be upserted. The method will perform the bulk upsert operation using the provided table name and objects and return an OpResult object.
Return Value¶
This operation returns an instance of the OpResult
class which represents an object containing the result of operation and also an array containing objectId
values of updated or inserted objects. The OpResult
object can be used as an argument in other operations in the same transaction (same UnitOfWork
instance). For more information see the Operation Result chapter of this guide.
Example¶
The example below uses the value of the "objectId"
property of each object in the invocation to search the "Person"
data table for objects that exist and can be updated. If objects are found in the table, they are updated accordingly, while any new objects that do not already exist in the table are inserted as new records.
Map<String, Object> person1 = new HashMap<>();
person1.put( "name", "Peter" );
person1.put( "email", "peter@yourmail.com" );
person1.put( "age", 55 );
Map<String, Object> person2 = new HashMap<>();
person2.put( "name", "John" );
person2.put( "email", "john@yourmail.com" );
person2.put( "age", 34 );
List<Map<String, Object>> persons = new ArrayList<>();
persons.add( person1 );
persons.add( person2 );
UnitOfWork unitOfWork = new UnitOfWork();
unitOfWork.bulkUpsert( "Person", persons );
UnitOfWorkResult result = unitOfWork.execute();
Person person1 = new Person();
person1.setName( "Peter" );
person1.setEmail( "peter@yourmail.com" );
person1.setAge( 55 );
Person person2 = new Person();
person2.setName( "John" );
person2.setEmail( "john@yourmail.com" );
person2.setAge( 34 );
List<Person> persons = new ArrayList<>();
persons.add( person1 );
persons.add( person2 );
UnitOfWork unitOfWork = new UnitOfWork();
unitOfWork.bulkUpsert( persons );
UnitOfWorkResult result = unitOfWork.execute();
Argument | Description |
---|---|
operation id |
Unique identifier of the current operation which can be used later on in other subsequent operation within the same transaction. Refer to the Operation Result topic for more information. |