Backendless Support
 

Updating object's related collections using API

Data objects stored in Backendless may have related objects through one-to-one or one-to-many collections.

When objects are retrieved on the client side, these relations are materialized as collections of data in object's fields or properties. 

Consider the following data object:

 

The Person table has the addresses column which is a one-to-many collection with the Address table.

The Person table schema is as the following: 

The related Address objects are shown below: 

At the code level, there is Person class defined as:
import java.util.Date;
import java.util.List;
public class Person
{
 public int age;
 public String name;
 public Date birthdate;
 public List<Address> addresses;
}
And the related Address class is defined as:
package com.mbaas.sample;
public class Address
{
 public String street;
 public String city;
}

The code below loads the Person object with the related Address objects. It prints out the number of Address objects, deletes one from the collection and saves the Person object back. Once it is saved and the updated instance is returned, the code prints out the number of related addresses to confirm one was indeed deleted:

Asynchronous API sample: (Android and Plain Java):
final AsyncCallback<Person> updateResponder = new AsyncCallback<Person>()
{
 @Override
 public void handleResponse( Person updatedPerson )
 {
 System.out.println( "Received updated object. Name - " + updatedPerson.name + ", relations - " + updatedPerson.addresses.size() );
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
 }
};
List<String> relations = new ArrayList<String>();
relations.add( "addresses" );
Backendless.Data.of( Person.class ).findFirst( relations, new AsyncCallback<Person>()
{
 @Override
 public void handleResponse( Person person )
 {
 System.out.println( "Loaded object. Name - " + person.name + ", relations - " + person.addresses.size() );
 person.addresses.remove( 0 );
 Backendless.Data.of( Person.class ).save( person, updateResponder );
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
 }
} );
Synchronous API sample (Plain Java only):
List<String> relations = new ArrayList<String>();
relations.add( "addresses" );
Person person = Backendless.Data.of( Person.class ).findFirst( relations );
System.out.println( "Loaded object. Name - " + person.name + ", relations - " + person.addresses.size() );
person.addresses.remove( 0 );
Person updatedPerson = Backendless.Data.of( Person.class ).save( person );
System.out.println( "Received updated object. Name - " + updatedPerson.name + ", relations - " + updatedPerson.addresses.size() );
The code produces the following output:
Loaded object. Name - Frankie, relations - 2
Received updated object. Name - Frankie, relations - 1

IMPORTANT:  Backendless supports ANY KIND of relation update. 

A developer's Backendless code could add new related objects, remove some (as shown above), change data inside of the related objects, etc. All of these changes will be persisted when the parent (Person) object is saved.

Is article helpful?