Skip to content

Related Objects

Establishing object relationship

The process of establishing a relationship between multiple objects in Backendless 3.x is by saving the parent object which references its child(ren) in the same object hierarchy/tree. Version 5 changes the mechanism by introducing a dedicated operation which establishes a relationship between the objects. The example below stores two objects in Backendless Data service: Contact and Address and then sets up a relationship between them:

Important

The sample code uses synchronous (blocking) API. As a result, it must be executed in a non-UI thread.

Version 3.x

IDataStore<Map> contactStorage = Backendless.Data.of( "Contact" );
IDataStore<Map> addressStorage = Backendless.Data.of( "Address" );

HashMap joeThePlumber = new HashMap();
joeThePlumber.put( "name", "Joe" );
joeThePlumber.put( "age", 27 );
joeThePlumber.put( "phone", "1-972-5551212" );
joeThePlumber.put( "title", "Plumber" );
joeThePlumber.put( "___class", "Contact" );

HashMap address = new HashMap();
address.put( "street", "123 Main St." );
address.put( "city", "Denver" );
address.put( "state", "Colorado" );
address.put( "___class", "Address" );

Map savedContact = contactStorage.save( joeThePlumber );
Map savedAddress = addressStorage.save( address );

List<Map> addresses = new ArrayList<Map>();
addresses.add( savedAddress );
savedContact.put( "addresses", addresses );

contactStorage.save( savedContact );

Version 5.x

IDataStore<Map> contactStorage = Backendless.Data.of( "Contact" );
IDataStore<Map> addressStorage = Backendless.Data.of( "Address" );

HashMap joeThePlumber = new HashMap();
joeThePlumber.put( "name", "Joe" );
joeThePlumber.put( "age", 27 );
joeThePlumber.put( "phone", "1-972-5551212" );
joeThePlumber.put( "title", "Plumber" );
joeThePlumber.put( "___class", "Contact" );

HashMap address = new HashMap();
address.put( "street", "123 Main St." );
address.put( "city", "Denver" );
address.put( "state", "Colorado" );
address.put( "___class", "Address" );

Map savedContact = contactStorage.save( joeThePlumber );
Map savedAddress = addressStorage.save( address );

List<Map> addresses = new ArrayList<Map>();
addresses.add( savedAddress );

// for 1:1 relations, use the ":1" notation instead of ":n"
contactStorage.setRelation( savedContact, "addresses:Address:n", addresses );

Deleting/breaking object relationship

Similar to establishing a relationship between objects, Backendless 3.x relied on the update operation which sends the parent object to the server with an updated list of child objects. Any children removed from the collection on the client-side, are removed on the server as a result of the update call. Version 5 introduces a dedicated method to remove a relationship between objects. The example below removes a relationship between a Contact object and the first related Address object:

Version 3.x

IDataStore<Map> contactStorage = Backendless.Data.of( "Contact" );
Map parentObject = contactStorage.findById( "XXXX-XXXX-XXXX-XXXXX" );

List<String> relationNames = new ArrayList<String>();
relationNames.add( "addresses" );
contactStorage.loadRelations( parentObject, relationNames );

List<Map> addresses = (List<Map>) parentObject.get( "addresses" );
addresses.remove( 0 );

parentObject.put( "addresses", addresses );
contactStorage.save( parentObject );

Version 5.x

IDataStore<Map> contactStorage = Backendless.Data.of( "Contact" );

// load parent object
Map parentObject = contactStorage.findById( "XXXX-XXXX-XXXX-XXXXX" );

// create query builder to load related/child objects
LoadRelationsQueryBuilder<Map<String, Object>> relationsQueryBuilder;
relationsQueryBuilder = LoadRelationsQueryBuilder.ofMap();
relationsQueryBuilder.setRelationName( "addresses" );

String parentObjectId = (String) parentObject.get( "objectId" );

// retrieve children for the "addresses" relation
List<Map<String, Object>> addresses = contactStorage.loadRelations( parentObjectId, 
                                                                    relationsQueryBuilder );

// create a list of child objects for which to break the relation
List<Map> relationToBreak = new ArrayList<Map>();
relationToBreak.add( addresses.get( 0 ) );

// delete the relation between the parent object and children for the "addresses" column
contactStorage.deleteRelation( parentObject, "addresses", relationToBreak );

Additional resources: