Blog

How to add client class to data table mapping

by on May 11, 2015

Data tables created in Backendless and the client-side classes you write or obtain through Backendless code generation have a direct correlation. By default, a table and the corresponding client-side class have the same name. This is true for all Backendless SDKs that support object-oriented data access. This includes Android, Java, Objective-C, Swift, JavaScript, ActionScript, and .NET. For example, consider the following data table:

The table has two relation columns: “locations” and “owner”. The former references a collection of objects (one-to-many relation) from the Location table:

The “owner” column from the Restaurant table references an object from the built-in Users table. Also, notice that the Location table has a relation column for the Menu table.

The classes representing these tables would look as shown below:

Restaurant class (the code below is a “compressed” version of the class. You can see full source code listing in the github repository of the Restaurant-To-Go project):

    package com.backendless.samples.restaurant.entities;
    import com.backendless.Backendless;
    import com.backendless.BackendlessCollection;
    import com.backendless.BackendlessUser;
    import com.backendless.async.callback.AsyncCallback;
    import com.backendless.persistence.BackendlessDataQuery;
    import java.io.Serializable;
    public class Restaurant implements Serializable
    {
      private String name;
      private String ownerId;
      private String objectId;
      private String cuisine;
      private java.util.Date updated;
      private java.util.Date created;
      private java.util.List<Location> locations;
      private BackendlessUser owner;
      // skipping getters and setters for brevity
    }

    Notice the class contains a Java property for every single column in the data table. This also applies to the relations, such as the “locations” and the “owner” fields.

    #import "Backendless.h"
    #import "Restaurant.h"
    #import "Location.h"
    @implementation Restaurant
    @end

    import Foundation
    class Restaurant : NSObject {
        var name : String?
        var ownerId : String?
        var objectId : String?
        var updated : NSDate?
        var created : NSDate?
        var cuisine : String?
        var locations : [Location]?
        var owner : BackendlessUser?
    }

    The Location class is defined as (see the full source code listing on Github):

      package com.backendless.samples.restaurant.entities;
      import com.backendless.Backendless;
      import com.backendless.BackendlessCollection;
      import com.backendless.async.callback.AsyncCallback;
      import com.backendless.persistence.BackendlessDataQuery;
      import java.io.Serializable;
      public class Location implements Serializable
      {
        private String streetAddress;
        private String country;
        private String objectId;
        private java.util.Date updated;
        private java.util.Date created;
        private String ownerId;
        private String city;
        private String phoneNumber;
        private Menu menu;
        // skipping getters and setters for brevity
      }

      #import "Backendless.h"
      #import "Location.h"
      @implementation Location
      @end

      import Foundation
      class Location : NSObject {
          var streetAdress : String?
          var country : String?
          var objectId : String?
          var updated : NSDate?
          var created : NSDate?
          var ownerId : String?
          var city : String?
          var phoneNumber : String?
      }

      The mapping between the table definition and the source code can be done by hand or automatically using the Backendless source code generator. When written by hand, it is very important to observe the naming conversion for the table and column names.