Backendless Support
 

Saving Objects with Relations and Dynamic Schema Creation

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.

The example is written in Java, but it will work the same way in Android or any other language which we support.

You can download the complete project with all the code using the URL below:
https://backendless.com/documentation/samples/SaveWithRelations.zip

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

package com.mbaas.samples.dto;
import java.util.ArrayList;
import java.util.List;
public class Order
{
 private int orderNumber;
 private String orderName;
 private List<OrderItem> orderItems;
 public String getOrderName()
 {
 return orderName;
 }
 public void setOrderName( String orderName )
 {
 this.orderName = orderName;
 }
 public int getOrderNumber()
 {
 return orderNumber;
 }
 public void setOrderNumber( int orderNumber )
 {
 this.orderNumber = orderNumber;
 }
 public void addOrderItem( OrderItem orderItem )
 {
 if( orderItems == null )
 orderItems = new ArrayList<OrderItem>();
 orderItems.add( orderItem );
 }
 public List<OrderItem> getOrderItems()
 {
 return orderItems;
 }
 public void setOrderItems( List<OrderItem> orderItems )
 {
 this.orderItems = orderItems;
 }
}

The OrderItem class referenced in Order looks like this:

package com.mbaas.samples.dto;
public class OrderItem
{
 private String name;
 private double price;
 private int quantity;
 public String getName()
 {
 return name;
 }
 public void setName( String name )
 {
 this.name = name;
 }
 public double getPrice()
 {
 return price;
 }
 public void setPrice( double price )
 {
 this.price = price;
 }
 public int getQuantity()
 {
 return quantity;
 }
 public void setQuantity( int quantity )
 {
 this.quantity = quantity;
 }
}

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

package com.mbaas.samples;
import com.backendless.Backendless;
import com.mbaas.samples.dto.Order;
import com.mbaas.samples.dto.OrderItem;
public class Main
{
 private static String APP_ID = "YOUR-APP-ID-GOES-HERE";
 private static String SECRET_KEY = "YOUR-ANDROID/JAVA-SECRET-KEY-GOES-HERE";
 private static String VERSION = "v1";
 public static void main( String[] args )
 {
 Backendless.initApp( APP_ID, SECRET_KEY, VERSION );
 Order order = new Order();
 OrderItem orderItem1 = new OrderItem();
 orderItem1.setName( "Printer" );
 orderItem1.setQuantity( 1 );
 orderItem1.setPrice( 99 );
 OrderItem orderItem2 = new OrderItem();
 orderItem2.setName( "Paper" );
 orderItem2.setQuantity( 10 );
 orderItem2.setPrice( 19 );
 order.addOrderItem( orderItem1 );
 order.addOrderItem( orderItem2 );
 order.setOrderName( "Office Supplies" );
 order.setOrderNumber( 1 );
 Backendless.Data.of( Order.class ).save( order );
 System.out.println( "Order has been saved" );
 }
}

Once you run the code and see the output from System.out.println at the end of the method, 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:

Is article helpful?