Saving Data Objects

Top  Previous  Next

The API to save an object can be used for two separate scenarios: if an object has been previously saved, it is updated in the data store, otherwise it is saved (created). The save operation checks if the object has objectId assigned by the server. in that case, the object is updated, otherwise it is created in the Backendless data store. In case when there is no table for the persisted object, Backendless creates one and maps table's columns to the object's properties.

The objectId property is automatically assigned to all persisted objects when they are initially saved. See the Data Object section for details on objectId.

 

Synchronous Method:

public Map Backendless.Persistence.of( "TABLE-NAME" ).save( Map entity ) throws BackendlessException
public <E> E Backendless.Persistence.of( E ).save( E entity ) throws BackendlessException

Asynchronous Method:

public Map void Backendless.Persistence.of( "TABLE-NAME" ).save( Map entity, AsyncCallback<E> responder )
public <E> void Backendless.Persistence.of( E ).save( E entity, AsyncCallback<E> responder )

where:

TABLE-NAME- Name of the table where the object represented by java.util.Map will be saved.
E- Java class of the data object to save.
entity- Java object to persist, must be of type E or java.util.Map (depending on the method used).
responder- a responder object which will receive a callback when the method successfully saves the object or if an error occurs. Applies  to the asynchronous method only.

Return Value:

The synchronous method returns the saved object. The asynchronous call receives the return value through a callback executed on the AsyncCallback object.

Example:

 

public void saveNewContact()
{
  HashMap contact = new HashMap();
  contact.put( "name", "Jack Daniels" );
  contact.put( "age", 147 );
  contact.put( "phone", "777-777-777" );
  contact.put( "title", "Favorites" );
  
  // save object synchronously
  Map savedContact = Backendless.Persistence.of( "Contact" ).save( contact );

  // save object asynchronously
  Backendless.Persistence.of( "Contact" ).save( contact, new AsyncCallback<Map>() {
      public void handleResponse( Map response )
      {
        // new Contact instance has been saved
      }

      public void handleFault( BackendlessFault fault )
      {
        // an error has occurred, the error code can be retrieved with fault.getCode()
      }
    });
}
Consider the following class:
package com.sample;

public class Contact
{
  private String objectId;
  private String name;
  private int age;
  private String phone;
  private String title;

  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;
  }
}
The following code saves a new instance of the Contact class:
public void saveNewContact()
{
  Contact contact = new Contact();
  contact.setName( "Jack Daniels" );
  contact.setAge( 147 );
  contact.setPhone( "777-777-777" );
  contact.setTitle( "Favorites" );

  // save object synchronously
  Contact savedContact = Backendless.Persistence.save( contact );

  // save object asynchronously
  Backendless.Persistence.save( contact, new AsyncCallback<Contact>() {
      public void handleResponse( Contact response )
      {
        // new Contact instance has been saved
      }

      public void handleFault( BackendlessFault fault )
      {
        // an error has occurred, the error code can be retrieved with fault.getCode()
      }
    });
}

 

 


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.: