Skip to content

Saving Single Object

Blocking API

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

Non-Blocking API

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

where:

Argument                Description
TABLE-NAME Name of the table where the object represented by

.util.Map will be saved.

Argument                Description
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.Data.of( "Contact" ).save( contact );

  // save object asynchronously
  Backendless.Data.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.Data.of( Contact.class).save( contact );

  // save object asynchronously
  Backendless.Data.of( Contact.class ).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()
      }
    });
}

Codeless Reference

data_service_saving_object

where:

Argument                Description
table name Name of the data table where a new record must be saved.
object An object to save in the database. Object properties must match the names of the table columns. The object must not have the objectId property.
return result Optional parameter. When this box is checked, the operation returns the saved object with the objectId property.

Returns the saved object with the objectId property assigned by Backendless

Consider the following structure of the data table called employees:
data_service_example_saving_object_1

For demonstration purposes, the data table presented above has three custom columns: name, position, and phoneNumber. The objectId is a system column that contains unique identifiers of the records in the table. When a new record is saved to the table, the system assigns a unique objectId to it. The objectid is used in various operations as an access point to a specific record.

The example below saves a new object to the employees data table:

data_service_example_saving_object_2

The result of this operation will look as shown below after the Codeless logic runs:

data_service_example_saving_object_3

The operation described above has returned the newly saved object:

data_service_example_saving_object_4