Skip to content

Set Relation with objects

API request replaces related objects with the ones identified in the API call. Child objects must be explicitly defined by referencing their IDs.

Blocking API

Integer result = Backendless.Persistence.of( "TABLE-NAME" ).setRelation( 
                                                   Map parentObject,
                                                   String relationColumnName,
                                                   Collection<Map> children );
Integer result = Backendless.Persistence.of( E ).setRelation( 
                                                   E parentObject,
                                                   String relationColumnName,
                                                   Collection<T> children );

Non-Blocking API

Backendless.Persistence.of( "TABLE-NAME" ).setRelation( 
                                             Map parentObject,
                                             String relationColumnName,
                                             Collection<Map> children,
                                             AsyncCallback<Integer> callback );
Backendless.Persistence.of( E ).setRelation( 
                                   E parentObject,
                                   String relationColumnName,
                                   Collection<T> children,
                                   AsyncCallback;<Integer> callback );

where:

Argument                Description
TABLE-NAME Name of the table where the parent object is stored.
E Java class of the parent object. The class name identifies the table where the parent object is stored.
parentObject The object which will be assigned related children for relatedColumnName. When this argument is an instance of java.util.Map (for the map-based approach), it must contain the "objectId" property.
relationColumnName Name of the column identifying the relation. Objects from the children collection will be set as related for the  column in parentObject. The column name may optionally include table name separated by the colon character as well as cardinality which determines the type of relationship (one to one or one to many) (see the note below):

Important

If the column does not exist in the parent table at the time when the API is called, the value of the "relationColumnName " argument must include the name of the child table separated by colon and the cardinality notation. The cardinality is expressed as ":1 " for one-to-one relations and ":n " for one-to-many relations. For example, the value of "myOrder:Order:1 " will create a one-to-one relation column "myOrder " in the parent table. The column will point to the Order child table. Likewise, the value of "myOrder:Order:n " will create a one-to-many relation column "myOrder " pointing to the Order table.

Argument                Description
children A collection of child objects to set into the relation identified by relatedColumnName. For the one-to-one relations the collection must contain one element.
callback A responder object which will receive a callback when the relation has been set or if an error occurs. Applies to the asynchronous method only.

Return Value

Number of child objects set into the relation. The asynchronous call receives the return value through a callback executed on the AsyncCallback object.

Example

The example below sets a relation for a one-to-one column named address declared in the Person table. The column must be declared in the table prior to the execution of the request shown below. This necessity is explained by missing table name qualifier in the relationColumnName argument - notice the relation column is "address". If the argument value were "address:Address:1", then the column would be created automatically.
relation-column

HashMap<String, Object> parentObject = new HashMap<String, Object>();
parentObject.put( "objectId", "41230622-DC4D-204F-FF5A-F893A0324800" );

HashMap<String, Object> childObject = new HashMap<String, Object>();
childObject.put( "objectId", "3464C734-F5B8-09F1-FFD3-18647D12E700" );

ArrayList<Map> children = new ArrayList<Map>();
children.add( childObject );

Backendless.Data.of( "Person" ).setRelation( parentObject, "address", children, 
                                             new AsyncCallback<Integer>()
{
  @Override
  public void handleResponse( Integer response )
  {
    Log.i( "MYAPP", "relation has been set" );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.e( "MYAPP", "server reported an error - " + fault.getMessage() );
  }
} );
Person personObject = // personObject retrieval is out of scope in this example
Address addressObject = // addressObject retrieval is out of scope in this example

ArrayList<Address> addressCollection = new ArrayList<Address>();
addressCollection.add( addressObject );

Backendless.Data.of( Person.class ).setRelation( personObject, "address", addressCollection,
        new AsyncCallback<Integer>()
        {
          @Override
          public void handleResponse( Integer response )
          {
            Log.i( "MYAPP", "relation has been set");
          }

          @Override
          public void handleFault( BackendlessFault fault )
          {
            Log.e( "MYAPP", "server reported an error - " + fault.getMessage() );
          }
        } );

Codeless Reference

The update operation presented further replaces existing objects relations with the new ones.

data_set_add_relation_condition_example_7

where:

Argument                Description
table name Name of the table where which contains the parent object as identified by parent object.
parent object Id of the object for which the relation will be created/set.
relation name Name of the column which identifies the relation within the parent table (identified as table-name). The column name may optionally include table name separated by the colon character as well as cardinality which determines the type of relationship (one to one or one to many) (see the note below):

If the column does not exist in the parent table at the time when the API is called, the value of the "relationColumnName" argument must include the name of the child table separated by colon and the cardinality notation. The cardinality is expressed as ":1" for one-to-one relations and ":n" for one-to-many relations. For example, the value of "myOrder:Order:1" will create a one-to-one relation column "myOrder" in the parent table. The column will point to the Order child table. Likewise, the value of "myOrder:Order:n" will create a one-to-many relation column "myOrder" pointing to the Order table.
children A list containing unique identifiers(objectId) of the children objects that are used to update existing relations of the parent object. The objectId can be a string value or a number.

Returns the number of newly set object relations.

The operation requires two data tables to update existing relations. Consider the first object with one-to-many relations(skills column) in the parent data table called employees:
data_set_add_relation_object_example_1

By clicking the value (1:N Relations) in the skills column of the parent data table presented above, you get redirected to the child data table called uniqueSkills, where you can see the related children objects:

data_set_add_relation_object_example_3

Suppose, you want to set new relations between the parent object in the employees data table and the third/fifth objects(having values REST and C++) in the uniqueSkills data table presented below:

data_set_add_relation_object_example_2

The example below updates the current relations for the parent object. Children objects that are used to update existing relations:

  1. (skill: REST, objectId: DA9EC928-4B1A-4F57-8C2E-4402D42FE8C9)

  2. (skill: C++, objectId: EA8D494F-50D4-445B-8F4E-F6EBC4C39D9F)

data_set_relation_object_example_1

As you can see, this operation has replaced the old relations(objects having Javascript and Java values) with the new ones(objects having C++ and REST values):

data_set_relation_object_example_2