Relations (Save/Update)

Top  Previous  Next

Backendless Data Service supports a complete set of CRUD (create, retrieve, update, delete) operations for related objects. Consider the following class diagram:

relational-data-example-uml

Notice the PhoneBook entity references Contact through two properties - "owner" (the one to one relationship) and "contacts" (the one to many relationship). Each contact entity also references Address. These entities will be used to demonstrate how Backendless handles related entities in all basic data persistence operations. Consider the following class definitions for these entities:

The Map-based approach does not require you defining classes for the objects stored in Backendless. Instead your code can use java.util.Map to to store and retrieve objects in/from Backendless.
Consider the following class definitions for the entities from the diagram: Person class:
package com.sample.entity;

import java.util.List;

public class PhoneBook
{
  private String objectId;
  private Contact owner;
  private List<Contact> contacts;

  public String getObjectId()
  {
    return objectId;
  }

  public void setObjectId( String objectId )
  {
    this.objectId = objectId;
  }

  public Contact getOwner()
  {
    return owner;
  }

  public void setOwner( Contact owner )
  {
    this.owner = owner;
  }

  public List<Contact> getContacts()
  {
    return contacts;
  }

  public void setContacts( List<Contact> contacts )
  {
    this.contacts = contacts;
  }
}
Contact class:
package com.sample.entity;

import java.util.Date;

public class Contact
{
  private String objectId;
  private String name;
  private int age;
  private String phone;
  private String title;
  private Address address;
  private Date updated;

  public String getObjectId()
  {
    return objectId;
  }

  public void setObjectId( String objectId )
  {
    this.objectId = objectId;
  }

  public String getName()
  {
    return name;
  }

  public void setName( String name )
  {
    this.name = name;
  }

  public int getAge()
  {
    return age;
  }

  public void setAge( int age )
  {
    this.age = age;
  }

  public String getPhone()
  {
    return phone;
  }

  public void setPhone( String phone )
  {
    this.phone = phone;
  }

  public String getTitle()
  {
    return title;
  }

  public void setTitle( String title )
  {
    this.title = title;
  }

  public Address getAddress()
  {
    return address;
  }

  public void setAddress( Address address )
  {
    this.address = address;
  }

  public Date getUpdated()
  {
    return updated;
  }

  public void setUpdated( Date updated )
  {
    this.updated = updated;
  }
}
Address class:
package com.sample.entity;

public class Address
{
  private String street;
  private String city;
  private String state;

  public String getStreet()
  {
    return street;
  }

  public void setStreet( String street )
  {
    this.street = street;
  }

  public String getCity()
  {
    return city;
  }

  public void setCity( String city )
  {
    this.city = city;
  }

  public String getState()
  {
    return state;
  }

  public void setState( String state )
  {
    this.state = state;
  }
}

Consider the following examples of saving objects with related data:

Constructing PhoneBook with the Owner and no Contacts:
phonebook-contact-address

HashMap address = new HashMap();
address.put( "___class", "Address" );
address.put( "street", "TN 55" );
address.put( "city", "Lynchburg" );
address.put( "state", "Tennessee" );

HashMap owner = new HashMap();
owner.put( "___class", "Contact" );
owner.put( "name", "Jack Daniels" );
owner.put( "age", 147 );
owner.put( "phone", "777-777-777" );
owner.put( "title", "Favorites" );
owner.put( "address", address );

HashMap phoneBook = new HashMap();
phoneBook.put( "owner", owner );

Map savedPhoneBook = Backendless.Persistence.of( "PhoneBook" ).save( phoneBook );
Address address = new Address();
address.setStreet( "TN 55" );
address.setCity( "Lynchburg" );
address.setState( "Tennessee" );

Contact owner = new Contact();
owner.setName( "Jack Daniels" );
owner.setAge( 147 );
owner.setPhone( "777-777-777" );
owner.setTitle( "Favorites" );
owner.setAddress( address );

PhoneBook phoneBook = new PhoneBook();
phoneBook.setOwner( owner );
PhoneBook savedPhoneBook = Backendless.Persistence.save( phoneBook );

Constructing PhoneBook with the Owner and two Contacts:
phonebook-contacts-owner

HashMap address = new HashMap();
address.put( "___class", "Address" );
address.put( "street", "TN 55" );
address.put( "city", "Lynchburg" );
address.put( "state", "Tennessee" );

HashMap owner = new HashMap();
owner.put( "___class", "Contact" );
owner.put( "name", "Jack Daniels" );
owner.put( "age", 147 );
owner.put( "phone", "777-777-777" );
owner.put( "title", "Favorites" );
owner.put( "address", address );

HashMap contact1Address = new HashMap();
contact1Address.put( "___class", "Address" );
contact1Address.put( "street", "Main St" );
contact1Address.put( "city", "Smallville" );
contact1Address.put( "state", "Kansas" );

HashMap contact1 = new HashMap();
contact1.put( "___class", "Contact" );
contact1.put( "name", "Clark Kent" );
contact1.put( "age", 75 );
contact1.put( "phone", "111-111-111" );
contact1.put( "title", "Super heroes" );
contact1.put( "address", contact1Address );

HashMap contact2Address = new HashMap();
contact2Address.put( "___class", "Address" );
contact2Address.put( "street", "Cavern-X" );
contact2Address.put( "city", "Sedona" );
contact2Address.put( "state", "Arizona" );

HashMap contact2 = new HashMap();
contact2.put( "___class", "Contact" );
contact2.put( "name", "Wade Winston Wilson" );
contact2.put( "age" 22 );
contact2.put( "phone", "222-222-222" );
contact2.put( "title", "Super heroes" );
contact2.put( "address", contact2Address );

List<HashMap> contacts = new ArrayList<HashMap>();
contacts.add( contact1 );
contacts.add( contact2 );

HashMap phoneBook = new HashMap();
phoneBook.put( "owner", owner );
phoneBook.put( "contacts", contacts );
Map savedPhoneBook = Backendless.Persistence.of( "PhoneBook" ).save( phoneBook );
Address address = new Address();
address.setStreet( "TN 55" );
address.setCity( "Lynchburg" );
address.setState( "Tennessee" );

Contact owner = new Contact();
owner.setName( "Jack Daniels" );
owner.setAge( 147 );
owner.setPhone( "777-777-777" );
owner.setTitle( "Favorites" );
owner.setAddress( address );

Address contact1Address = new Address();
contact1Address.setStreet( "Main St" );
contact1Address.setCity( "Smallville" );
contact1Address.setState( "Kansas" );

Contact contact1 = new Contact();
contact1.setName( "Clark Kent" );
contact1.setAge( 75 );
contact1.setPhone( "111-111-111" );
contact1.setTitle( "Super heroes" );
contact1.setAddress( contact1Address );

Address contact2Address = new Address();
contact2Address.setStreet( "Cavern-X" );
contact2Address.setCity( "Sedona" );
contact2Address.setState( "Arizona" );

Contact contact2 = new Contact();
contact2.setName( "Wade Winston Wilson" );
contact2.setAge( 22 );
contact2.setPhone( "222-222-222" );
contact2.setTitle( "Super heroes" );
contact2.setAddress( contact2Address );

List<Contact> contacts = new ArrayList<Contact>();
contacts.add( contact1 );
contacts.add( contact2 );

PhoneBook phoneBook = new PhoneBook();
phoneBook.setOwner( owner );
phoneBook.setContacts( contacts );
PhoneBook savedPhoneBook = Backendless.Persistence.save( phoneBook );

Add Contact to an existing PhoneBook (uses the "savedPhoneBook" object created at the end of the example above):
add-contact-address-to-phonebook

// skipped code for brevity.. 
// use the code from the example above
Map savedPhoneBook = Backendless.Persistence.of( "PhoneBook" ).save( phoneBook );

HashMap contact2Address = new HashMap();
contact2Address.put( "___class", "Address" );
contact2Address.put( "street", "Cavern-X" );
contact2Address.put( "city", "Sedona" );
contact2Address.put( "state", "Arizona" );

HashMap contact2 = new HashMap();
contact2.put( "___class", "Contact" );
contact2.put( "name", "Wade Winston Wilson" );
contact2.put( "age" 22 );
contact2.put( "phone", "222-222-222" );
contact2.put( "title", "Super heroes" );
contact2.put( "address", contact2Address );

// Add contact to the saved phone book
((List<Map>)savedPhoneBook.get( "contacts )).add( contact2 );

// And save the phone book again - the new object will be 
// properly created in the Backendless data store
Map updatedPhoneBook = Backendless.Persistence.of( "PhoneBook" ).save( savedPhoneBook );
// skipped code for brevity.. 
// use the code from the example above
PhoneBook savedPhoneBook = Backendless.Persistence.save( phoneBook );

Address contact2Address = new Address();
contact2Address.setStreet( "Cavern-X" );
contact2Address.setCity( "Sedona" );
contact2Address.setState( "Arizona" );

Contact contact2 = new Contact();
contact2.setName( "Wade Winston Wilson" );
contact2.setAge( 22 );
contact2.setPhone( "222-222-222" );
contact2.setTitle( "Super heroes" );
contact2.setAddress( contact2Address );

// Add contact to the saved phone book
savedPhoneBook.getContacts().add( contact2 );

// And save the phone book again - the new object will be 
// properly created in the Backendless data store
PhoneBook updatedPhoneBook = Backendless.Persistence.save( savedPhoneBook );

Update a property in the aggregated object (update the phone number in the "owner" of PhoneBook) and save PhoneBook:
update-contact

// use any of the samples above to the point when PhoneBook is saved,
// then use the savedPhoneBook instance:
((Map) savedPhoneBook.get( "owner" )).put( "phone", "555-555-555" );
Map updatedPhoneBook = Backendless.Persistence.of( "PhoneBook" ).save( savedPhoneBook );
// use any of the samples above to the point when PhoneBook is saved,
// then use the savedPhoneBook instance:
savedPhoneBook.getOwner().setPhone( "555-555-555" );
PhoneBook updatedPhoneBook = Backendless.Persistence.save( savedPhoneBook );

Removing one contact from PhoneBook, adding another and re-saving PhoneBook:
remove-contact-add-another

// use any of the samples above to the point when PhoneBook is saved,
// then use the savedPhoneBook instance:
HashMap contact2Address = new HashMap();
contact2Address.put( "___class", "Address" );
contact2Address.put( "street", "Cavern-X" );
contact2Address.put( "city", "Sedona" );
contact2Address.put( "state", "Arizona" );

HashMap contact2 = new HashMap();
contact2.put( "___class", "Contact" );
contact2.put( "name", "Wade Winston Wilson" );
contact2.put( "age", 22 );
contact2.put( "phone", "222-222-222" );
contact2.put( "title", "Super heroes" );
contact2.put( "address", contact2Address );

((List<Map>) savedPhoneBook.get( "contacts" )).remove( 0 );
((List<Map>) savedPhoneBook.get( "contacts" )).add( contact2 );
Map updatedPhoneBook = Backendless.Persistence.of( "PhoneBook" ).save( savedPhoneBook );
// use any of the samples above to the point when PhoneBook is saved,
// then use the savedPhoneBook instance:
Address contact2Address = new Address();
contact2Address.setStreet( "Cavern-X" );
contact2Address.setCity( "Sedona" );
contact2Address.setState( "Arizona" );

Contact contact2 = new Contact();
contact2.setName( "Wade Winston Wilson" );
contact2.setAge( 22 );
contact2.setPhone( "222-222-222" );
contact2.setTitle( "Super heroes" );
contact2.setAddress( contact2Address );

savedPhoneBook.getContacts().remove( 0 );
savedPhoneBook.getContacts().add( contact2 );
PhoneBook updatedPhoneBook = Backendless.Persistence.save( savedPhoneBook );

 


Please let us know how we can improve the documentation by leaving a comment. All technical questions should be posted to the Backendless Support forum. We do not respond to the technical questions on the documentation pages.: