Message:

Subscribe rss
Blog categories
All | Backendless features
How to Use Code Generation to Simplify Working with Data in Backendless
April 9, 2019 by Oleg Vyalyh

How to Use Code Generation to Work with Backendless Data

In Java, entity objects are classes that represent data from your table. From an object-oriented perspective, these objects are built to encapsulate your data in the real-world problem domain. There are two different ways you can work with entity objects:

  • Using the HashMap approach (like plain representation of column names and their values);
  • Using the data binding approach, where the particular type is used for every table.

The first approach is more flexible, but it doesn’t offer security for the type values. Also, it’s possible to make a mistake setting the data. It also requires you to remember the names of the columns. 

The second approach is much more appealing in terms of the readability and understandability of the code. It also offers type safety.

The main drawback here is the necessity to write data binding objects (or entity objects if you prefer) that will represent the particular table. And what if you have a very large number of tables in your application, and every table consists of many columns? The amount of such tedious work could be significant. But don’t fret, Backendless is already doing it for you!

Let’s take a look at an example.

1) First, create a table. Set the schema to match the following screenshot. You may also download it with the prepared data and import it into your Backendless app.

Create Data Table in Backendless Console

2) Next, go to the “Code Generation” section and download the generated entity classes for your tables.

Code Generation with Java Classes in Backendless

You also can also download the archive later as it is saved in your Files.

Download Doc from Files in Backendless Console

3) Unpack the downloaded archive completely, or extract only the necessary files.

In our example we need only the file called Car.java

Example Java File Unpacked

4) Add the class file to your project.

5) That’s all! Now you can look through and try simple code examples, as below.

Create new Car and save it.

Remove first Car from the table asynchronously.

Change the condition of the last car.

Find by id.

Get list of cars using some conditions.

Closing

In this article, we showed you how to generate Java classes from your data and then access them with code. Thanks for reading.

Share this post
Tweet about this on TwitterShare on FacebookGoogle+