Updating a single object¶
Important
This operation updates a single object in the database. To update more than one object, use the bulkUpdate operation documented in the Updating multiple objects chapter of this guide.
To add this operation to a Unit Of Work instance, use the following methods:
The methods are organized by how the object to be updated is specified in the API. As you can see below, there are several ways to reference the object which will be updated in the database: explicit object reference, which is either using a Map
or a custom class or by using a result of a previous operation:
// Saves the object represented by the "objectToSave" argument in the database
// The object must have a server assigned value for the objectId property.
// The object will be saved in a data table with the "tableName" name.
// Values in the "objectToSave" map will overwrite the ones in the database.
unitOfWorkInstance.update( String tableName, Map<String, Object> objectToSave );
// Saves the object represented by the "objectToSave" argument in
// the database. The YourCustomClass class must have either
// the get/set methods for the objectId property or a public
// string field called objectId. The object must exist in a
// data table which corresponds to the YourCustomClass class.
// For example if YourCustomClass is called Person, then the
// objectToSave must be stored in the Persom table.
unitOfWorkInstance.update( YourCustomClass objectToSave );
// Update an object represented by the "result" argument. The argument
// is a result of a previous operation in the same transaction.
// Since this operation expects a single object, the "result"
// argument must represent a single object as well. This means it
// can be a result from any of the following previous operations:
// - saving a single object
// - updating a single object
// The "changes" argument is a key/value map of changes which will
// be updated in the object represented by "result". The keys in
// "changes" correspond to the columns in the data table where
// the updated object is stored.
OpResult unitOfWorkInstance.update( OpResult result,
Map<String, Object> changes );
// Same as the above, but instead of a collection/map of "changes",
// this method accepts only one key/value pair in the form of
// propertyName and propertyValue. The propertyName argument
// is a property name in the object to be updated that will be
// updated with the value in the propertyValue argument.
OpResult unitOfWorkInstance.update( OpResult result,
String propertyName,
Object propertyValue );
// Update an object represented by the "result" argument. The argument
// is a reference to an object from a result of a previous operation
// in the same transaction. Since this operation expects a single object,
// the "result" argument must represent a single object as well.
// This means it can be obtained from any of the following previous
// operations:
// - saving multiple objects
// - retrieving objects
// The "changes" argument is a key/value map of changes which will
// be updated in the object represented by "result". The keys in
// "changes" correspond to the columns in the data table where
// the updated object is stored.
OpResult unitOfWorkInstance.update( OpResultValueReference result,
Map<String, Object> changes );
// Same as the above, but instead of a collection/map of "changes",
// this method accepts only one key/value pair in the form of
// propertyName and propertyValue. The propertyName argument
// is a property name in the object to be updated that will be
// updated with the value in the propertyValue argument.
OpResult unitOfWorkInstance.update( OpResultValueReference result,
String propertyName,
Object propertyValue );
Return Value¶
All methods listed above return an instance of the OpResult
class which represents the updated object in the database. 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 examples below demonstrate the usage of the operation for all possible ways to reference the object that should be updated. This is the what the data table looks like before the transaction runs:
UnitOfWork unitOfWork = new UnitOfWork();
Map<String, Object> changes = new HashMap<>();
changes.put( "objectId", "37ADFFA8-39E2-73A5-FF44-651EF5C4E900" );
changes.put( "orderStatus", "complete" );
changes.put( "deliveryDate", new SimpleDateFormat( "dd/MM/yyyy" ).parse( "20/03/2020" ));
unitOfWork.update( "Order", changes );
unitOfWork.execute( new AsyncCallback<UnitOfWorkResult>()
{
@Override
public void handleResponse( UnitOfWorkResult result )
{
Log.i( "MYAPP", "transaction complete - " + result );
}
@Override
public void handleFault( BackendlessFault fault )
{
Log.e( "MYAPP", "Server reported an error " + fault );
}
} );
UnitOfWork unitOfWork = new UnitOfWork();
Order orderInstance = new Order();
orderInstance.setObjectId( "37ADFFA8-39E2-73A5-FF44-651EF5C4E900" );
orderInstance.setOrderStatus( "complete" );
orderInstance.setDeliveryDate( new SimpleDateFormat("dd/MM/yyyy").parse( "20/03/2020" ));
unitOfWork.update( orderInstance );
unitOfWork.execute( new AsyncCallback<UnitOfWorkResult>()
{
@Override
public void handleResponse( UnitOfWorkResult result )
{
System.out.println( "transaction complete - " + result );
}
@Override
public void handleFault( BackendlessFault fault )
{
System.out.println( "Server reported an error " + fault );
}
} );
package com.company;
import java.util.Date;
public class Order
{
private String objectId;
private Date deliveryDate;
private String orderStatus;
public String getObjectId()
{
return objectId;
}
public void setObjectId( String objectId )
{
this.objectId = objectId;
}
public Date getDeliveryDate()
{
return deliveryDate;
}
public void setDeliveryDate( Date deliveryDate )
{
this.deliveryDate = deliveryDate;
}
public String getOrderStatus()
{
return orderStatus;
}
public void setOrderStatus( String orderStatus )
{
this.orderStatus = orderStatus;
}
}
UnitOfWork unitOfWork = new UnitOfWork();
// compose query builder
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
// set the query condition
queryBuilder.setWhereClause( "orderId = '031820-CV1'" );
// we need only one object, this will help to speed it up
queryBuilder.setPageSize( 1 );
// add the find operation to the transaction
OpResult findResult = unitOfWork.find( "Order", queryBuilder );
// get a reference to the first object (order) from the result
OpResultValueReference orderObjectRef = findResult.resolveTo( 0 );
// compose a map of changes - notice the objectId is not needed there
Map<String, Object> changes = new HashMap<>();
changes.put( "orderStatus", "complete" );
changes.put( "deliveryDate", new SimpleDateFormat("dd/MM/yyyy").parse( "20/03/2020" ));
// add the update operation to the transaction
unitOfWork.update( orderObjectRef, changes );
// run the transaction
unitOfWork.execute( new AsyncCallback<UnitOfWorkResult>()
{
@Override
public void handleResponse( UnitOfWorkResult result )
{
System.out.println( "transaction complete - " + result );
}
@Override
public void handleFault( BackendlessFault fault )
{
System.out.println( "Server reported an error " + fault );
}
} );
This is what the same object in the data table looks like after the transaction runs: