Skip to content

Update Parent and Child

The example below demonstrates how the Deep Save API can be used to update both the parent and the child objects in the database. The sample retrieves a Product and the related Manufacturer objects then it changes property values in both objects and saves the changes with a single Deep Save API call. The objects before the code runs are:

The Product object:

product-with-manuf-before-deepsave

The Manufacturer object:

manuf-before-deepsave \

The example uses blocking API for code brevity. When using in an Android application, keep in mind the blocking API cannot be used on the mail UI thread. Either run it in a separate thread or use the non-blocking API.

DataQueryBuilder queryBuilder = DataQueryBuilder.create();
// load the related manufacturer object
queryBuilder.addRelated( "manufacturer" );
// get the specific product
queryBuilder.setWhereClause( "name = 'iPhone X'" );

// use get( 0 ) to get the first object from the response
Map<String, Object> iPhone = Backendless.Data.of( "Product" ).find( queryBuilder ).get( 0 );

// update values in both parent (Order) and child (Manufacturer) objects
iPhone.put( "price", 1300 );
Map<String, Object> manufacturer = (Map<String, Object>) iPhone.get( "manufacturer" );
manufacturer.put( "industry", "Computer Hardware, Software and Consumer Electronics" );

// save both objects back in the database
Backendless.Data.of( "Product" ).deepSave( iPhone );

DataQueryBuilder queryBuilder = DataQueryBuilder.create();
// load the related manufacturer object
queryBuilder.addRelated( "manufacturer" );
// get the specific product
queryBuilder.setWhereClause( "name = 'iPhone X'" );

// use get( 0 ) to get the first object from the response
Product iPhone = Backendless.Data.of( Product.class ).find( queryBuilder ).get( 0 );

// update values in both parent (Order) and child (Manufacturer) objects
iPhone.price = 1300;
iPhone.manufacturer.industry = "Computer Hardware, Software and Consumer Electronics";

// save both objects back in the database
Backendless.Data.of( Product.class ).deepSave( iPhone );
Product class. Public fields are used for brevity - JavaBean get/set methods are supported and recommended.
public class Product
{
  public String objectId;
  public String name;
  public double price;
  public Manufacturer manufacturer;
}
Manufacturer class. Public fields are used for brevity - JavaBean get/set methods are supported and recommended.
public class Manufacturer
{
  public String objectId;
  public String name;
  public String industry;
}

After the sample code runs, the objects in the database will appear as shown below:

The Product object:

product-after-deepsave

The Manufacturer object:

manuf-after-deepsave