Skip to content

Delete Objects from relation

The API removes specific objects from a relationship with their parent.

Blocking API

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

Non-Blocking API

Backendless.Persistence.of( "TABLE-NAME" ).deleteRelation( 
                                             Map parentObject,
                                             String relationColumnName,
                                             Collection<Map> children,
                                             AsyncCallback<Integer> callback );
Backendless.Persistence.of( E ).deleteRelation( 
                                   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 for which the relation with the specified children will be deleted. 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. Relationship between the specified objects from the children collection will be deleted for the column in parentObject.
children A collection of child objects for which the relationship with the parentObject will be deleted.
callback a responder object which will receive a callback when the relation has been deleted or if an error occurs. Applies to the asynchronous method only.

Return Value

Number of child objects for which the relationship has been deleted. The asynchronous call receives the return value through a callback executed on the AsyncCallback object.

Example

The example below deletes a relation between a Person object and its children. The child objects are referenced explicitly in the API call (see the object IDs in the collection as "XXXXX-XXXXX-XXXXX-XXXXX" ``and`` "ZZZZ-ZZZZ-ZZZZZ-ZZZZZ").The relation column is address.

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

HashMap<String, Object> childObject1 = new HashMap<String, Object>();
childObject1.put( "objectId", "XXXX-XXXX-XXXX-XXXXX" );

HashMap<String, Object> childObject2 = new HashMap<String, Object>();
childObject2.put( "objectId", "ZZZZ-ZZZZ-ZZZZZ-ZZZZZ" );

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

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

  @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 addressObject1 = // addressObject retrieval is out of scope in this example
Address addressObject2 = // addressObject retrieval is out of scope in this example
ArrayList<Address> addressCollection = new ArrayList<Address>();
addressCollection.add( addressObject1 );
addressCollection.add( addressObject2 );

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

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