Skip to content

Adding to a relation

This operation adds child objects to a parent in a relationship. Unlike the Setting a relation operation, which removes any existing child objects and sets the provided children, this operation adds the children to the existing ones.

Specific Children

There are multiple "flavors" of this operation, but in its core, it must reference a parent object and one or more child objects. The parent object must have a valid objectIdvalue. The APIs are grouped below by how the parent object is identified - using objectId, as a map, as an instance of a custom class or as a result of a previous operation in the same transaction:

// Add the objects identified with objectId values in the childrenObjectId array
// to the children of the parent object identified with parentObjectId. The parent 
// obbject must be in the table with the parentTableName name. The relationship 
// column between the parent and child objects is columnName. In other words, 
// for the relationship represented through the columnName column, aadd the child
// objects to the parent object. Both child and parent objects are identified with 
// objectId values.
unitOfWorkInstance.addToRelation( String parentTableName, 
                                  String parentObjectId, 
                                  String columnName, 
                                  String[] childrenObjectId );

// Same as the above with the only difference is the child objects are instances
// of a custom class (YourCustomClassForChild). It is important that 
// YourCustomClassForChild matches the child table name. For example, if the 
// relationship is between tables Person and Address, then the 
// YourCustomClassForChild class must be "Address".
unitOfWorkInstance.addToRelation( String parentTableName, 
                                  String parentObjectId, 
                                  String columnName, 
                                  YourCustomClassForChild[] children );

// Same as the first signature above with the only difference is the child 
// objects are instances of the Map interface. It is important that each Map object 
// has a key/value pair for the "objectId" property with a valid value.
unitOfWorkInstance.addToRelation( String parentTableName, 
                                  String parentObjectId, 
                                  String columnName, 
                                  List<Map<String, Object>> children );

// Add the objects represented by the "children" argument to the children collection
// of the parent object. The parent object is identified by its objectId with the 
// parentObjectId argument. The parent object must be stored in a table with the
// parentTableName name. The "children" argument is a result of a previous operation
// in the same transaction. It must represent either a collection of string values
// (objectIds) or a collection of objects. This is possible with the following 
// previous operations:
//   - Retrieving objects from the database
//   - Saving multiple objects in the database
// It is important that all child objects referenced by the children argument exist
// in the child table that columnName points to.
unitOfWorkInstance.addToRelation( String parentTableName, 
                                  String parentObjectId, 
                                  String columnName, 
                                  OpResult children );
// Add the objects from the childrenObjectId array to the children of 
// parentObject. The array must contain objectId values of the child objects. 
// The parent object must be in the table with the parentTableName name. 
// The relationship column between the parent and child objects is columnName. 
// In other words, for the relationship represented through the columnName column, 
// add the child objects to the existing collection of children of the parent 
// object. The parent object is a map of key/value pairs. One of the pairs 
// must be objectId with the corresponding value. The children are identified 
// with their objectId values.
unitOfWorkInstance.addToRelation( String parentTableName, 
                                  Map<String, Object> parentObject, 
                                  String columnName, 
                                  String[] childrenObjectId );

// Same as the above with the only difference is the child objects are instannces
// of a custom class (YourCustomClassForChild). It is important that 
// YourCustomClassForChild matches the child table name. For example, if the 
// relationship is between tables Person and Address, then the 
// YourCustomClassForChild class must be "Address".
unitOfWorkInstance.addToRelation( String parentTableName, 
                                  Map<String, Object> parentObject, 
                                  String columnName,                                                            
                                  YourCustomClassForChild[] children );

// Same as the first signature above with the only difference is the child 
// objects are instances of the Map interface. It is important that each Map 
// object has a key/value pair for the "objectId" property with a valid value.
unitOfWorkInstance.addToRelation( String parentTableName, 
                                  Map<String, Object> parentObject, 
                                  String columnName, 
                                  List<Map<String, Object>> children );

// Add the objects represented by the "children" argument to the children of 
// the parent object. The parent object is identified by a Map object which 
// must have a key/value pair for the objectId property. The parent object must
// be stored in a table with the parentTableName name. The "children" argument is
// a result of a previous operation in the same transaction. It must represent 
// either a collection of string values (objectIds) or a collection of objects. 
// This is possible with the following previous operations:
//   - Retrieving objects from the database
//   - Saving multiple objects in the database
// It is important that all child objects referenced by the children argument 
// exist in the child table that columnName points to.
unitOfWorkInstance.addToRelation( String parentTableName, 
                                  Map<String, Object> parentObject, 
                                  String columnName, 
                                  OpResult children );
// Add the objects identified with objectId values in the childrenObjectId 
// array to the children collection of parentObject. The relationship 
// column between the parent and child objects is columnName. Notice the
// parent object is an  instance of YourCustomClassForParent. The class 
// must declare public get/set methods for the objectId property or have
// a public objectId field. The child objects are identified with their
// objectId values. It is important that YourCustomClassForParent class
// matches the parent table name. Since the parent object is an instance
// of YourCustomClassForParent, it means the parent table where the parent 
// object is stored must match the name of the class. 
// For example, if YourCustomClassForParent is "Person", it means 
// Backendless wiil:
// 1. Look for the parent object in the Person table
// 2. Locate a column with the columnName name in the Person table
// 3. Using the column definition, Backendless will identify the child table
// 4. Backendless will retrieve the child objects from the child table using 
//    the provided objectId values.
// 5. Backendless wiill addthe child objects to the relationship with the
//    the parent object.
unitOfWorkInstance.addToRelation( YourCustomClassForParent parentObject, 
                                  String columnName, 
                                  String[] childrenObjectId );

// Same as the above with the only differennce is the child objects are 
// represented through a custom class. It is important that 
// YourCustomClassForChild matches the child table name. For example, 
// if the relationship is between tables Person and Address, then the 
// YourCustomClassForChild class must be "Address".
unitOfWorkInstance.addToRelation( YourCustomClassForParent parentObject,
                                  String columnName, 
                                  YourCustomClassForChild[] children );

// Same as the first signature above with the only difference is the child 
// objects are instances of the Map interface. It is important that each 
// Map object has a key/value pair for the "objectId" property with a 
// valid value. 
unitOfWorkInstance.addToRelation( YourCustomClassForParent parentObject,
                                  String columnName, 
                                  List<Map<String, Object>> children );

// Same as the first signature above with the only difference is the children
// argument is a result of a previous operation in the same transaction. It 
// must represent either a collection of string values (objectIds) or a 
// collection of objects. This is possible with the following previous 
// operations:
//   - Retrieving objects from the database
//   - Saving multiple objects in the database
// It is important that all child objects referenced by the children argument 
// exist in the child table that columnName points to.
unitOfWorkInstance.addToRelation( YourCustomClassForParent parentObject,
                                  String columnName, 
                                  OpResult children );
// Add the children identified with their objectId values from the 
// childrenObjectId array to the related children collection of 
// parentObject. The parentObject value is a result of a previous 
// operation in the same transaction. These operations can be:
//   - Saving a single object in the database
//   - Updating a single object in the database
// The relationship is identified through the column with the name in the 
// columnName argument. The column must be declared in the same table where 
// parentObject comes from. It is important that all child objects in the 
// childrenObjectId array exist in the child table that columnName points to.
unitOfWorkInstance.addToRelation( OpResult parentObject, 
                                  String columnName, 
                                  String[] childrenObjectId );

// Same as the above with the difference that the child objects are instances 
// of the YourCustomClassForChild class. The class used for child objects 
// must match the name of the child table. Suppose the relationship is 
// between tables Person and Address. In this case, the name of the class 
// must be Address.
unitOfWorkInstance.addToRelation( OpResult parentObject, 
                                  String columnName, 
                                  YourCustomClassForChild[] children );

// Same as the first signature above with the only difference is the child 
// objects are instances of the Map interface. It is important that each Map 
// object has a key/value pair for the "objectId" property with a valid value.
unitOfWorkInstance.addToRelation( OpResult parentObject,
                                  String columnName, 
                                  List<Map<String, Object>> children );

// In this operation both the parent and the children objects are results of 
// two separate previous operations in the same transaction. The parent 
// object must be a result of any of the following operations:
//   - Saving a single object in the database
//   - Updating a single object in the database
// The relationship is identified through the column with the name in the 
// columnName argument. The column must be declared in the same table where 
// parentObject comes from. The children argument can be a result of the 
// following operations:
//   - Retrieving objects from the database
//   - Saving multiple objects in the database
// It is important that all child objects referenced by the children argument 
// exist in the child table that columnName points to.
unitOfWorkInstance.addToRelation( OpResult parentObject, 
                                  String columnName, 
                                  OpResult children );

// This method and the ones listed below identify the parent object the 
// same way - using the OpResultValueReference class. This is a special 
// class which references an object within a result of annother operation 
// withhin the same transaction. For example, a result of another operation 
// can be a collection of objects. That collection can be returned from 
// either the "Retrieving objects" or "Saving multiple objetcs" operations. 
// All transaction operations return an OpResult object. Using the OpResult 
// API you can obtain a single object which can be used as parent in the 
// operations listed below. When a single object from a collection represented 
// by OpResult is referenced, it is represented by the OpResultValueReference 
// class.
unitOfWorkInstance.addToRelation( OpResultValueReference parentObject, 
                                  String columnName, 
                                  String[] childrenObjectId );

unitOfWorkInstance.addToRelation( OpResultValueReference parentObject, 
                                  String columnName, 
                                  List<YourCustomClassForChildren> children );

unitOfWorkInstance.addToRelation( OpResultValueReference parentObject, 
                                  String columnName, 
                                  List<Map<String, Object>> children );


unitOfWorkInstance.addToRelation( OpResultValueReference parentObject, 
                                  String columnName, 
                                  OpResult children );

Children with a Query

There are APIs where the child objects can be identified with a query using a where clause. These APIs run a query to get the child objects and then sets them to the parent in the relation. Again the APIs are grouped by how the parent object is identified:

// Add the objects identified by whereClauseForChildren as children of the parent object identified 
// with parentObjectId. The parent object must be in the table with the parentTableName name. 
// The relationship column between the parent and child objects is columnName. The whereClauseForChildren
// query is executed in the child table that columnName points to.
unitOfWorkInstance.OpResult addToRelation( String parentTable, 
                                           String parentObjectId, 
                                           String columnName, 
                                           String whereClauseForChildren);
// Add the objects identified by whereClauseForChildren as children of the parent object represented
// by the parentObject map. The parent object must be in the table with the parentTableName name. 
// The relationship column between the parent and child objects is columnName.
// The whereClauseForChildren query is executed in the child table that the columnName column points to.
unitOfWorkInstance.addToRelation( String parentTable, 
                                  Map<String, Object> parentObject, 
                                  String columnName, 
                                 String whereClauseForChildren )
// Add the objects identified by whereClauseForChildren as children of the parent object represented
// as an instance of the YourCustomClassForParent class. The relationship column between the parent 
// and child objects is columnName. 
// Since the parent object is an instance of YourCustomClassForParent, it means the parent table where
// the parent object is stored must match the name of the class. For example, if YourCustomClassForParent
// is "Person", it means Backendless wiil:
// 1. Look for the parent object in the Person table.
// 2. Locate a column with the columnName name in the Person table.
// 3. Using the column definition, Backendless will identify the child table.
// 4. Backendless will run the whereClauseForChildren query in the child table to identify the child objects.
// 5. Backendless wiil add the child objects to the relationship with the parent object.
// The YourCustomClassForParent class must declare public get/set methods for the objectId property or have a public 
// objectId field. The child objects are identified with their objectId values. 
unitOfWorkInstance.addToRelation( YourCustomClassForParent parentObject,
                                  String columnName, 
                                  String whereClauseForChildren );
// Add the objects identified by the whereClauseForChildren query 
// to the already existing collection of children of the parent 
// object.The parentObject value is a result of a previous operation
// in the same transaction. These operations can be:
//   - Saving a single object in the database
//   - Updating a single object in the database
// The relationship is identified through the column with the name
// in the "columnName" argument. The column must be declared in the
// same table where parentObject comes from. For example, if the 
// parent object is stored in a table called "Person", 
// Backendless wiil:
// 1. Look for the parent object in the Person table
// 2. Locate a column with the columnName name in the Person table
// 3. Using the column definition, Backendless will identify the 
//    child table
// 4. Backendless will run the whereClauseForChildren query in the 
//    child table to identify the child objects
// 5. Backendless wiil add the child objects to the relationship 
//    with the parent object.
unitOfWorkInstance.addToRelation( OpResult parentObject, 
                                  String columnName, 
                                  String whereClauseForChildren );

// Same as the above except the parentobject is a reference to an 
// object within a result of annother operation in the same 
// transaction. For example, a result of another operation can be
// a collection of objects. That collection can be returning from 
// either the "Retrieving objects" or "Saving multiple objetcs"
// operations. Using the OpResult API you can obtain a single object
//  which can be used as parent in this operation. When a single 
// object from a collection represented by OpResult is referenced, 
// it is represented by the OpResultValueReference class.
unitOfWorkInstance.addToRelation( OpResultValueReference parentObject, 
                                  String columnName, 
                                  String whereClauseForChildren );

Return Value

The operation returns an OpResult object which represents the result of this operation - number of child objects added to the relation.

Examples

The examples are organized by two levels: first is how the parent object is represented in the client program, then by the type of the child collection.

Parent object is represented as

** The child objects are represented as**

UnitOfWork unitOfWork = new UnitOfWork();

// This is an array for child objects.
// They are referenced using objectId values.
// The example hard-codes the array, but in
// your code you'd get the objectId values
// from your data model or using a query
String[] giftIds = new String[] {
        "EE3BF4B5-DB88-1425-FF89-CC11B7707500",
        "0CF23E36-FCC0-4E04-FF3E-8B67E6E27200",
        "DFFEDE1D-E423-2472-FF71-26EEC3F23700"
};

// this is the objectId of the parent object.
String personObjectId = "E7AD83E0-1B4E-D250-FF46-61BFAB18D700";

// the name of the table where the parent object is stored
String parentTableName = "Person";

// the name of the relation column
String relationColumnName = "wishlist";

// add the addToRelation call to the transaction
unitOfWork.addToRelation( parentTableName,
        personObjectId,
        relationColumnName,
        giftIds );

// run the transaction
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();

// This is a list of gift objects. These objects
// will be children in the relationship. As you
// can see the objects are constructed manually
// and their objectId's are set, however, in your
// application they would be loaded from the
// server. This is done here to keep the example
// more succinct.
List<Map<String, Object>> gifts = new ArrayList<>();

Map<String, Object> iPad = new HashMap<>();
iPad.put( "objectId", "EE3BF4B5-DB88-1425-FF89-CC11B7707500" );
gifts.add( iPad );

Map<String, Object> iPhone = new HashMap<>();
iPhone.put( "objectId", "0CF23E36-FCC0-4E04-FF3E-8B67E6E27200" );
gifts.add( iPhone );

// this is the objectId of the parent object.
String personObjectId = "E7AD83E0-1B4E-D250-FF46-61BFAB18D700";

// the name of the table where the parent object is stored
String parentTableName = "Person";

// the name of the relation column
String relationColumnName = "wishlist";

// add the addToRelation call to the transaction
unitOfWork.addToRelation( parentTableName,
            personObjectId,
            relationColumnName,
            gifts );

// run the transaction
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();

// This is a list of gift objects. These objects
// will be children in the relationship. As you
// can see the objects are constructed manually
// and their objectId's are set, however, in your
// application they would be loaded from the
// server. This is done here to keep the example
// more succinct.
List<Gift> gifts = new ArrayList<>();

Gift iPad = new Gift();
iPad.setObjectId( "EE3BF4B5-DB88-1425-FF89-CC11B7707500" );
gifts.add( iPad );

Gift iPhone = new Gift();
iPhone.setObjectId( "0CF23E36-FCC0-4E04-FF3E-8B67E6E27200" );
gifts.add( iPhone );

// this is the objectId of the parent object.
String personObjectId = "E7AD83E0-1B4E-D250-FF46-61BFAB18D700";

// the name of the table where the parent object is stored
String parentTableName = "Person";

// the name of the relation column
String relationColumnName = "wishlist";

// add the addToRelation call to the transaction
unitOfWork.addToRelation( parentTableName,
        personObjectId,
        relationColumnName,
        gifts );

// run the transaction
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 );
  }
} );
The Gift class is shown below:
package com.company;

public class Gift
{
  private String objectId;
  private String name;
  private double price;

  public String getObjectId()
  {
    return objectId;
  }

  public void setObjectId( String objectId )
  {
    this.objectId = objectId;
  }

  public String getName()
  {
    return name;
  }

  public void setName( String name )
  {
    this.name = name;
  }

  public double getPrice()
  {
    return price;
  }

  public void setPrice( double price )
  {
    this.price = price;
  }
}

UnitOfWork unitOfWork = new UnitOfWork();

// compose a query to fetch the gift objects
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause( "name in ( 'Apple iPad', 'Apple iPhone', 'Selfie Stick')" );

// add the find operation to the transaction
// notice the result of the operation is saved in a variable -
// it will be used further in the setRelation operation
OpResult gifts = unitOfWork.find( "Gift", queryBuilder );

// this is the objectId of the parent object.
String personObjectId = "E7AD83E0-1B4E-D250-FF46-61BFAB18D700";

// the name of the table where the parent object is stored
String parentTableName = "Person";

// the name of the relation column
String relationColumnName = "wishlist";

// add the addToRelation call to the transaction
unitOfWork.addToRelation( parentTableName,
        personObjectId,
        relationColumnName,
        gifts );

// run the transaction
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 );
  }
} );

** The child objects are represented as**

UnitOfWork unitOfWork = new UnitOfWork();

// This is an array for child objects.
// They are referenced using objectId values.
// The example hard-codes the array, but in
// your code you'd get the objectId values
// from your data model or using a query
String[] giftIds = new String[] {
        "EE3BF4B5-DB88-1425-FF89-CC11B7707500",
        "0CF23E36-FCC0-4E04-FF3E-8B67E6E27200",
        "DFFEDE1D-E423-2472-FF71-26EEC3F23700"
};

// this is the parent object. The only property that
// matters for the sake of establishing a relation
// is objectId. In your program you would be
// retrieving the object from the server. The
// technique shown here is for code brevity.
Map<String, Object> personObject = new HashMap<>();
personObject.put( "objectId", "E7AD83E0-1B4E-D250-FF46-61BFAB18D700" );

// the name of the table where the parent object is stored
String parentTableName = "Person";

// the name of the relation column
String relationColumnName = "wishlist";

// add the addToRelation call to the transaction
unitOfWork.addToRelation( parentTableName,
        personObject,
        relationColumnName,
        giftIds );

// run the transaction
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();

// This is a list of gift objects. These objects
// will be children in the relationship. As you
// can see the objects are constructed manually
// and their objectId's are set, however, in your
// application they would be loaded from the
// server. This is done here to keep the example
// more succinct.
List<Map<String, Object>> gifts = new ArrayList<>();

Map<String, Object> iPad = new HashMap<>();
iPad.put( "objectId", "EE3BF4B5-DB88-1425-FF89-CC11B7707500" );
gifts.add( iPad );

Map<String, Object> iPhone = new HashMap<>();
iPhone.put( "objectId", "0CF23E36-FCC0-4E04-FF3E-8B67E6E27200" );
gifts.add( iPhone );

// this is the parent object. The only property that
// matters for the sake of establishing a relation
// is objectId. In your program you would be
// retrieving the object from the server. The
// technique shown here is for code brevity.
Map<String, Object> personObject = new HashMap<>();
personObject.put( "objectId", "E7AD83E0-1B4E-D250-FF46-61BFAB18D700" );

// the name of the table where the parent object is stored
String parentTableName = "Person";

// the name of the relation column
String relationColumnName = "wishlist";

// add the addToRelation call to the transaction
unitOfWork.addToRelation( parentTableName,
        personObject,
        relationColumnName,
        gifts );

// run the transaction
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();

// This is a list of gift objects. These objects
// will be children in the relationship. As you
// can see the objects are constructed manually
// and their objectId's are set, however, in your
// application they would be loaded from the
// server. This is done here to keep the example
// more succinct.
List<Gift> gifts = new ArrayList<>();

Gift iPad = new Gift();
iPad.setObjectId( "EE3BF4B5-DB88-1425-FF89-CC11B7707500" );
gifts.add( iPad );

Gift iPhone = new Gift();
iPhone.setObjectId( "0CF23E36-FCC0-4E04-FF3E-8B67E6E27200" );
gifts.add( iPhone );

// this is the parent object. The only property that
// matters for the sake of establishing a relation
// is objectId. In your program you would be
// retrieving the object from the server. The
// technique shown here is for code brevity.
Map<String, Object> personObject = new HashMap<>();
personObject.put( "objectId", "E7AD83E0-1B4E-D250-FF46-61BFAB18D700" );

// the name of the table where the parent object is stored
String parentTableName = "Person";

// the name of the relation column
String relationColumnName = "wishlist";

// add the addToRelation call to the transaction
unitOfWork.addToRelation( parentTableName,
        personObject,
        relationColumnName,
        gifts );

// run the transaction
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 );
  }
} );
The Gift class is shown below:
package com.company;

public class Gift
{
  private String objectId;
  private String name;
  private double price;

  public String getObjectId()
  {
    return objectId;
  }

  public void setObjectId( String objectId )
  {
    this.objectId = objectId;
  }

  public String getName()
  {
    return name;
  }

  public void setName( String name )
  {
    this.name = name;
  }

  public double getPrice()
  {
    return price;
  }

  public void setPrice( double price )
  {
    this.price = price;
  }
}

UnitOfWork unitOfWork = new UnitOfWork();

// compose a query to fetch the gift objects
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause( "name in ( 'Apple iPad', 'Apple iPhone', 'Selfie Stick')" );

// add the find operation to the transaction
// notice the result of the operation is saved in a variable -
// it will be used further in the setRelation operation
OpResult gifts = unitOfWork.find( "Gift", queryBuilder );

// this is the parent object. The only property that
// matters for the sake of establishing a relation
// is objectId. In your program you would be
// retrieving the object from the server. The
// technique shown here is for code brevity.
Map<String, Object> personObject = new HashMap<>();
personObject.put( "objectId", "E7AD83E0-1B4E-D250-FF46-61BFAB18D700" );

// the name of the table where the parent object is stored
String parentTableName = "Person";

// the name of the relation column
String relationColumnName = "wishlist";

// add the addToRelation call to the transaction
unitOfWork.addToRelation( parentTableName,
        personObject,
        relationColumnName,
        gifts );

// 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 );
  }
} );

** The child objects are represented as**

UnitOfWork unitOfWork = new UnitOfWork();

// This is an array for child objects.
// They are referenced using objectId values.
// The example hard-codes the array, but in
// your code you'd get the objectId values
// from your data model or using a query
String[] giftIds = new String[] {
        "EE3BF4B5-DB88-1425-FF89-CC11B7707500",
        "0CF23E36-FCC0-4E04-FF3E-8B67E6E27200",
        "DFFEDE1D-E423-2472-FF71-26EEC3F23700"
};

// this is the parent object. The only property that
// matters for the sake of establishing a relation
// is objectId. In your program you would be
// retrieving the object from the server. The
// technique shown here is for code brevity.
Person personObject = new Person();
personObject.setObjectId( "E7AD83E0-1B4E-D250-FF46-61BFAB18D700" );

// the name of the relation column
String relationColumnName = "wishlist";

// add the addToRelation call to the transaction
unitOfWork.addToRelation(
        personObject,
        relationColumnName,
        giftIds );

// run the transaction
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();

// This is a list of gift objects. These objects
// will be children in the relationship. As you
// can see the objects are constructed manually
// and their objectId's are set, however, in your
// application they would be loaded from the
// server. This is done here to keep the example
// more succinct.
List<Map<String, Object>> gifts = new ArrayList<>();

Map<String, Object> iPad = new HashMap<>();
iPad.put( "objectId", "EE3BF4B5-DB88-1425-FF89-CC11B7707500" );
gifts.add( iPad );

Map<String, Object> iPhone = new HashMap<>();
iPhone.put( "objectId", "0CF23E36-FCC0-4E04-FF3E-8B67E6E27200" );
gifts.add( iPhone );

// this is the parent object. The only property that
// matters for the sake of establishing a relation
// is objectId. In your program you would be
// retrieving the object from the server. The
// technique shown here is for code brevity.
Person personObject = new Person();
personObject.setObjectId( "E7AD83E0-1B4E-D250-FF46-61BFAB18D700" );

// the name of the relation column
String relationColumnName = "wishlist";

// add the addToRelation call to the transaction
unitOfWork.addToRelation(
        personObject,
        relationColumnName,
        gifts );

// run the transaction
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();

// This is a list of gift objects. These objects
// will be children in the relationship. As you
// can see the objects are constructed manually
// and their objectId's are set, however, in your
// application they would be loaded from the
// server. This is done here to keep the example
// more succinct.
List<Gift> gifts = new ArrayList<>();

Gift iPad = new Gift();
iPad.setObjectId( "EE3BF4B5-DB88-1425-FF89-CC11B7707500" );
gifts.add( iPad );

Gift iPhone = new Gift();
iPhone.setObjectId( "0CF23E36-FCC0-4E04-FF3E-8B67E6E27200" );
gifts.add( iPhone );

// this is the parent object. The only property that
// matters for the sake of establishing a relation
// is objectId. In your program you would be
// retrieving the object from the server. The
// technique shown here is for code brevity.
Person personObject = new Person();
personObject.setObjectId( "E7AD83E0-1B4E-D250-FF46-61BFAB18D700" );

// the name of the relation column
String relationColumnName = "wishlist";

// add the addToRelation call to the transaction
unitOfWork.addToRelation(
        personObject,
        relationColumnName,
        gifts );

// run the transaction
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 );
  }
} );
The Gift class is shown below:
package com.company;

public class Gift
{
  private String objectId;
  private String name;
  private double price;

  public String getObjectId()
  {
    return objectId;
  }

  public void setObjectId( String objectId )
  {
    this.objectId = objectId;
  }

  public String getName()
  {
    return name;
  }

  public void setName( String name )
  {
    this.name = name;
  }

  public double getPrice()
  {
    return price;
  }

  public void setPrice( double price )
  {
    this.price = price;
  }
}

UnitOfWork unitOfWork = new UnitOfWork();

// compose a query to fetch the gift objects
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause( "name in ( 'Apple iPad', 'Apple iPhone', 'Selfie Stick')" );

// add the find operation to the transaction
// notice the result of the operation is saved in a variable -
// it will be used further in the setRelation operation
OpResult gifts = unitOfWork.find( "Gift", queryBuilder );

// this is the parent object. The only property that
// matters for the sake of establishing a relation
// is objectId. In your program you would be
// retrieving the object from the server. The
// technique shown here is for code brevity.
Person personObject = new Person();
personObject.setObjectId( "E7AD83E0-1B4E-D250-FF46-61BFAB18D700" );

// the name of the relation column
String relationColumnName = "wishlist";

// add the addToRelation call to the transaction
unitOfWork.addToRelation(
        personObject,
        relationColumnName,
        gifts );

// run the transaction
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 );
  }
} );

** The child objects are represented as**

UnitOfWork unitOfWork = new UnitOfWork();

// This is an array for child objects.
// They are referenced using objectId values.
// The example hard-codes the array, but in
// your code you'd get the objectId values
// from your data model or using a query
String[] giftIds = new String[] {
        "EE3BF4B5-DB88-1425-FF89-CC11B7707500",
        "0CF23E36-FCC0-4E04-FF3E-8B67E6E27200",
        "DFFEDE1D-E423-2472-FF71-26EEC3F23700"
};

// compose a query to retrieve the person
// from the database
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
// this is the query which identifies the person of interest,
// this person object will be used as the parent in the relationship
queryBuilder.setWhereClause( "name = 'John Doe' and age = 36" );

// add the object retrieval operation to the transaction
OpResult findResult = unitOfWork.find( "Person", queryBuilder );

// since "findResult" references a collection of objects,
// get the first one from the collection. The reason we need to
// do this is because the setRelation operation below
// expects only one parent object. We know the first object
// will be the parent, that's why the "resolveTo" index is 0.
OpResultValueReference paarentObjectRef = findResult.resolveTo( 0 );

// the name of the relation column
String relationColumnName = "wishlist";

// add the addToRelation call to the transaction
unitOfWork.addToRelation(
        paarentObjectRef,
        relationColumnName,
        giftIds );

// run the transaction
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();

// This is a list of gift objects. These objects
// will be children in the relationship. As you
// can see the objects are constructed manually
// and their objectId's are set, however, in your
// application they would be loaded from the
// server. This is done here to keep the example
// more succinct.
List<Map<String, Object>> gifts = new ArrayList<>();

Map<String, Object> iPad = new HashMap<>();
iPad.put( "objectId", "EE3BF4B5-DB88-1425-FF89-CC11B7707500" );
gifts.add( iPad );

Map<String, Object> iPhone = new HashMap<>();
iPhone.put( "objectId", "0CF23E36-FCC0-4E04-FF3E-8B67E6E27200" );
gifts.add( iPhone );

// compose a query to retrieve the person
// from the database
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
// this is the query which identifies the person of interest,
// this person object will be used as the parent in the relationship
queryBuilder.setWhereClause( "name = 'John Doe' and age = 36" );

// add the object retrieval operation to the transaction
OpResult findResult = unitOfWork.find( "Person", queryBuilder );

// since "findResult" references a collection of objects,
// get the first one from the collection. The reason we need to
// do this is because the setRelation operation below
// expects only one parent object. We know the first object
// will be the parent, that's why the "resolveTo" index is 0.
OpResultValueReference parentObjectRef = findResult.resolveTo( 0 );

// the name of the relation column
String relationColumnName = "wishlist";

// add the addToRelation call to the transaction
unitOfWork.addToRelation(
        parentObjectRef,
        relationColumnName,
        gifts );

// run the transaction
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();

// This is a list of gift objects. These objects
// will be children in the relationship. As you
// can see the objects are constructed manually
// and their objectId's are set, however, in your
// application they would be loaded from the
// server. This is done here to keep the example
// more succinct.
List<Gift> gifts = new ArrayList<>();

Gift iPad = new Gift();
iPad.setObjectId( "EE3BF4B5-DB88-1425-FF89-CC11B7707500" );
gifts.add( iPad );

Gift iPhone = new Gift();
iPhone.setObjectId( "0CF23E36-FCC0-4E04-FF3E-8B67E6E27200" );
gifts.add( iPhone );

// compose a query to retrieve the person
// from the database
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
// this is the query which identifies the person of interest,
// this person object will be used as the parent in the relationship
queryBuilder.setWhereClause( "name = 'John Doe' and age = 36" );

// add the object retrieval operation to the transaction
OpResult findResult = unitOfWork.find( "Person", queryBuilder );

// since "findResult" references a collection of objects,
// get the first one from the collection. The reason we need to
// do this is because the setRelation operation below
// expects only one parent object. We know the first object
// will be the parent, that's why the "resolveTo" index is 0.
OpResultValueReference parentObjectRef = findResult.resolveTo( 0 );

// the name of the relation column
String relationColumnName = "wishlist";

// add the addToRelation call to the transaction
unitOfWork.addToRelation(
        parentObjectRef,
        relationColumnName,
        gifts );

// run the transaction
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 );
  }
} );
The Gift class is shown below:
package com.company;

public class Gift
{
  private String objectId;
  private String name;
  private double price;

  public String getObjectId()
  {
    return objectId;
  }

  public void setObjectId( String objectId )
  {
    this.objectId = objectId;
  }

  public String getName()
  {
    return name;
  }

  public void setName( String name )
  {
    this.name = name;
  }

  public double getPrice()
  {
    return price;
  }

  public void setPrice( double price )
  {
    this.price = price;
  }
}

UnitOfWork unitOfWork = new UnitOfWork();

// compose a query to fetch the gift objects
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause( "name in ( 'Apple iPad', 'Apple iPhone', 'Selfie Stick')" );

// add the find operation to the transaction
// notice the result of the operation is saved in a variable -
// it will be used further in the setRelation operation
OpResult gifts = unitOfWork.find( "Gift", queryBuilder );

// compose a query to retrieve the person
// from the database
DataQueryBuilder parentQueryBuilder = DataQueryBuilder.create();
// this is the query which identifies the person of interest,
// this person object will be used as the parent in the relationship
parentQueryBuilder.setWhereClause( "name = 'John Doe' and age = 36" );

// add the object retrieval operation to the transaction
OpResult findResult = unitOfWork.find( "Person", parentQueryBuilder );

// since "findResult" references a collection of objects,
// get the first one from the collection. The reason we need to
// do this is because the setRelation operation below
// expects only one parent object. We know the first object
// will be the parent, that's why the "resolveTo" index is 0.
OpResultValueReference parentObjectRef = findResult.resolveTo( 0 );

// the name of the relation column
String relationColumnName = "wishlist";

// add the addToRelation call to the transaction
unitOfWork.addToRelation(
        parentObjectRef,
        relationColumnName,
        gifts );

// run the transaction
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 );
  }
} );