Basic Object Retrieval

Top  Previous  Next

Backendless supports multiple basic search operations. These include finding an object by ID, finding first or last object in the collection or retrieving the entire persisted collection. Each method is available in both synchronous and asynchronous versions:

Retrieving Data Objects

Synchronous Methods:

Retrieve data objects with the default paging setting from a table:
public BackendlessCollection<Map> Backendless.Persistence.of( "TABLE-NAME" ).find()
Find first data object from a table. The first data object is the first one saved in the data store:
public Map Backendless.Persistence.of( "TABLE-NAME" ).findFirst() 
Find last data object from a table. The last data object is the last one saved in the data store:
public Map Backendless.Persistence.of( "TABLE-NAME" ).findLast() 
Find a data object by its ID:
public Map Backendless.Persistence.of( "TABLE-NAME" ).findById( String objectId ) 
Retrieve data objects with the default paging setting from a table. Returned collection will contain objects of type E (the name of the class must match the name of the table):
public BackendlessCollection<E> Backendless.Persistence.of( E ).find() 
Find first data object of class E. The first data object is the first one saved in the data store:
public E Backendless.Persistence.of( E ).findFirst() 
Find last data object of type E. The last data object is the last one saved in the data store:
public E Backendless.Persistence.of( E ).findLast() 
Find a data object by its ID:
public E Backendless.Persistence.of( E ).findById( String objectId ) 

Asynchronous Methods:

Retrieve data objects with the default paging setting from a table:
public void Backendless.Persistence.of( "TABLE-NAME" ).find( AsyncCallback<BackendlessCollection<Map>> responder )
Find first data object from a table. The first data object is the first one saved in the data store:
public void Backendless.Persistence.of( "TABLE-NAME" ).findFirst( AsyncCallback<Map> responder )
Find last data object from a table. The last data object is the last one saved in the data store:
public void Backendless.Persistence.of( "TABLE-NAME" ).findLast( AsyncCallback<Map> responder ) 
Find a data object by its ID:
public void Backendless.Persistence.of( "TABLE-NAME" ).findById( String objectId, AsyncCallback<Map> responder ) 
Retrieve data objects with the default paging setting from a table. Returned collection will contain objects of type E (the name of the class must match the name of the table):
public void Backendless.Persistence.of( E ).find( AsyncCallback<BackendlessCollection<E>> responder );
Find first data object from a table. The name of the class E must match the name of the table. The first data object is the first one saved in the data store:
public void Backendless.Persistence.of( E ).findFirst( AsyncCallback<E> responder )
Find last data object from a table. The name of the class E must match the name of the table. The last data object is the last one saved in the data store:
public void Backendless.Persistence.of( E ).findLast( AsyncCallback<E> responder )
Find a data object by its ID. The name of the class E must match the name of the table:
public void Backendless.Persistence.of( E ).findById( String objectId, AsyncCallback<E> responder )

where:

TABLE-NAME- Name of the table from where the data is retrieved from.
E- Java class identifying the table where from where the data must be loaded from. For example, if the table is "Person", the argument should Person.class.
responder- a responder object which will receive a callback when the method successfully returns the result or if an error occurs. Applies to the asynchronous methods only.

Example:

The following code demonstrates various search queries:

Load contacts using default paging:

Synchronous call:
BackendlessCollection<Map> result = Backendless.Persistence.of( "Contact" ).find();
Asynchronous call:
Backendless.Persistence.of( "Contact" ).find( new AsyncCallback<BackendlessCollection<Map>>(){
  @Override
  public void handleResponse( BackendlessCollection<Map> foundContacts )
  {
    // every loaded object from the "Contact" table is now an individual java.util.Map
  }
  @Override
  public void handleFault( BackendlessFault fault )
  {
    // an error has occurred, the error code can be retrieved with fault.getCode()
  }
});

Find first contact:

Synchronous call:
Map firstContact = Backendless.Persistence.of( "Contact" ).findFirst();
Asynchronous call:
Backendless.Persistence.of( "Contact" ).findFirst( new AsyncCallback<Map>(){
  @Override
  public void handleResponse( Map contact )
  {
    // first contact instance has been found
  }
  @Override
  public void handleFault( BackendlessFault fault )
  {
    // an error has occurred, the error code can be retrieved with fault.getCode()
  }
});

Find last contact:

Synchronous call:
Map firstContact = Backendless.Persistence.of( "Contact" ).findLast();
Asynchronous call:
Backendless.Persistence.of( "Contact" ).findLast( new AsyncCallback<Map>(){
  @Override
  public void handleResponse( Map contact )
  {
    // last contact instance has been found
  }
  @Override
  public void handleFault( BackendlessFault fault )
  {
    // an error has occurred, the error code can be retrieved with fault.getCode()
  }
});

Find contact by objectId:

Synchronous call:
// Save a contact object first. Once it is saved, we will retrieve it by assigned objectId
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 );

// now retrieve the object using it's objectId
Map lastContact = Backendless.Persistence.of( "Contact" ).findById( savedContact.get( "objectId" ) );
Asynchronous call:
// Save a contact object first. Once it is saved, we will retrieve it by assigned objectId
HashMap contact = new HashMap();
contact.put( "name", "Jack Daniels" );
contact.put( "age", 147 );
contact.put( "phone", "777-777-777" );
contact.put( "title", "Favorites" );
 
Backendless.Persistence.of( "Contact" ).save( contact, new AsyncCallback<Map>()
{
  @Override
  public void handleResponse( Map savedContact )
  {
    // now that the object is saved and has objectId, retrieve it using "findById"
    Backendless.Persistence.of( Contact.class ).findById( savedContact.get( "objectId" ), 
                                                          new AsyncCallback<Map>() {
      @Override
      public void handleResponse( Map response )
      {
        // an object from the "Contact" table has been found by it's objectId
      }
      @Override
      public void handleFault( BackendlessFault fault )
      {
        // an error has occurred, the error code can be retrieved with fault.getCode()
      }
    } );
  }
  @Override
  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 demonstrates various search queries:

Load contacts using default paging:

Synchronous call:
BackendlessCollection<Contact> result = Backendless.Persistence.of( Contact.class ).find();
Asynchronous call:
Backendless.Persistence.of( Contact.class).find( new AsyncCallback<BackendlessCollection<Contact>>(){
  @Override
  public void handleResponse( BackendlessCollection<Contact> foundContacts )
  {
    // all Contact instances have been found
  }
  @Override
  public void handleFault( BackendlessFault fault )
  {
    // an error has occurred, the error code can be retrieved with fault.getCode()
  }
});

Find first contact:

Synchronous call:
Contact firstContact = Backendless.Persistence.of( Contact.class ).findFirst();
Asynchronous call:
Backendless.Persistence.of( Contact.class).findFirst( new AsyncCallback<Contact>(){
  @Override
  public void handleResponse( Contact contact )
  {
    // first contact instance has been found
  }
  @Override
  public void handleFault( BackendlessFault fault )
  {
    // an error has occurred, the error code can be retrieved with fault.getCode()
  }
});

Find last contact:

Synchronous call:
Contact firstContact = Backendless.Persistence.of( Contact.class ).findLast();
Asynchronous call:
Backendless.Persistence.of( Contact.class).findLast( new AsyncCallback<Contact>(){
  @Override
  public void handleResponse( Contact contact )
  {
    // last contact instance has been found
  }
  @Override
  public void handleFault( BackendlessFault fault )
  {
    // an error has occurred, the error code can be retrieved with fault.getCode()
  }
});

Find contact by objectId:

Synchronous call:
// Save a contact object first. Once it is saved, we will retrieve it by assigned objectId
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 );

// now retrieve the object using it's objectId
Contact lastContact = Backendless.Persistence.of( Contact.class ).findById( savedContact.getObjectId() );
Asynchronous call:
// Save a contact object first. Once it is saved, we will retrieve it by assigned objectId
Contact contact = new Contact();
contact.setName( "Jack Daniels" );
contact.setAge( 147 );
contact.setPhone( "777-777-777" );
contact.setTitle( "Favorites" );
 
Backendless.Persistence.save( contact, new AsyncCallback<Contact>()
{
  @Override
  public void handleResponse( Contact savedContact )
  {
    // now that the object is saved and has objectId, retrieve it using "findById"
    Backendless.Persistence.of( Contact.class ).findById( savedContact.getObjectId(), 
                                                          new AsyncCallback<Contact>() {
      @Override
      public void handleResponse( Contact response )
      {
        // a Contact instance has been found by objectId
      }
      @Override
      public void handleFault( BackendlessFault fault )
      {
        // an error has occurred, the error code can be retrieved with fault.getCode()
      }
    } );
  }
  @Override
  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.: