Backendless Support
 

Saving Objects with Relations and Dynamic Schema Creation

This topic highlights the issue of saving an object with relations in Backendless. Here the "code first" approach is used, which means data tables will not be creating 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 Backendless will create data tables corresponding to the classes used in the examples and it will be possible to see the data.

If a developer does not have a Backendless app (or Backendless backend), it can be easily create ones. It will be nesessary to login to the application and get the Application ID and Secret Key which are used by Backendless to identify a developer's own backend. This values can be obtained 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:

                                    FOR Objective-C and Swift implementations CLICK HERE

JAVA

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):
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:

JAVA

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 a developer runs the code and sees the output from the program, he/she should go to the app in Backendless Console.

Click the Data icon and it will appear the tables created by Backendless with the objects saved in them: 

Is article helpful?