Feature 1: Saving Objects with Relations and Dynamic Schema Creation | Backend as a Service Platform

Message:

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

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, Backendless 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 an account in Backendless, you can easily create one. You will need to login to your application and get the Application ID and the API Key which are used by Backendless to identify your app. 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 an API Key to the client-side code from this post if you decide to run it.

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:
saved order - Feature 1: Saving Objects with Relations and Dynamic Schema Creation

related orderitems - Feature 1: Saving Objects with Relations and Dynamic Schema Creation

Enjoy!

Share this post
Tweet about this on TwitterShare on FacebookGoogle+