Message:

Subscribe rss
Blog categories
All | Backendless features
Feature 1: Saving Objects with Relations and Dynamic Schema Creation
January 7, 2015 by markpiller
Spread the love

This is the first post in the “Blog a Feature a Day” series we’re starting today. In this post I will show how to save an object with relations in Backendless. I am using the “code first” approach, which means I will not be creating data tables in the backend. Instead the code will dictate to the backend what the data schema should look like. The example demonstrating the feature consists of two classes: Order and OrderItem. An instance of the Order class may contain a collection of OrderItem objects. The example will create an order, populate it with order items and save the order on the server. As a result, the Backend will create data tables corresponding to the classes used in the examples and you will be able to see the data.

If you do not have a Backendless app (or Backendless backend), you can easily create one. You will need to login to your application and get the Application ID and Secret Key which are used by Backendless to identify your own backend. You can obtain these values by following the steps below:

  1. Login to console, select an app and click the Manage icon.
  2. The Application Settings screen shows the application ID and secret keys for the client-side environments. Make sure to copy and paste the app id and Android Secret Key to the client-side code from this post if you decide to run it.

You can download the complete  project with all the code using the URLs below:
Java:
https://backendless.com/documentation/samples/SaveWithRelations.zip
iOS (Objective-C and Swift):
https://backendless.com/documentation/samples/F1SavingObjects.zip 

The Order class is shown below:

Notice the class does not have to implement any of the Backendless interfaces or extend any special classes, just a plain old Java object (POJO that is):

Order header:

Order class implementation:

Order.js:

The OrderItem class referenced in Order looks like this:

OrderItem header:

OrderItem implementation:

Finally, the main block of code which creates a few instances of OrderItem and puts them into an Order object:

Once you run the code and see the output from the program, go to your app in Backendless Console. Click the Data icon and you should see the tables created by Backendless with the objects saved in them:
order table - Feature 1: Saving Objects with Relations and Dynamic Schema Creation

orderitem table - Feature 1: Saving Objects with Relations and Dynamic Schema Creation

Enjoy!

Share this post
Tweet about this on TwitterShare on FacebookGoogle+