Intermediate: Data Management (Object Relational Mapping Database) in Harmony OS

Lokesh Suryan
4 min readJul 30, 2021

--

Introduction

Huawei provides various services for developers to make ease of development and provides best user experience to end users. In this article, we will cover Object Relational Mapping (ORM) database with Java in Harmony OS.

The Object Relational Mapping (ORM) database in Harmony OS on the basic of SQLite database provides a list of object-oriented APIs for adding, modifying, deleting and querying entities and relational data. To use the ORM database, you need to configure the entity modelling and mapping files in advance. As a result, without the need of writing complex SQL statements, you can operate the database by operating the mapped objects. This improves the database’s work efficiency. The ORM database can be used when we do not want to use complex SQL statements.

Working principle

Development Overview

You need to install DevEcho studio IDE and I assume that you have prior knowledge about the Harmony OS and java.

Hardware Requirements

  • A computer (desktop or laptop) running Windows 10.
  • A Huawei phone (with the USB cable), which is used for debugging.

Software Requirements

  • Java JDK installation package.
  • DevEcho studio installed.

Follows the steps.

  1. Create HarmonyOS Project.
  • Open DevEcho studio.
  • Click NEW Project, select a Project Templet.
  • Select ability template and click Next as per below image.
  • Enter Project and Package Name and click on Finish.

2. Once you have created the project, DevEco Studio will automatically sync it with Gradle files. Find the below image after synchronization is successful.

3. Update Permission and app version in config.json file as per your requirement, otherwise retain the default values.

4. Create New Ability, as follows.

5. Development Procedure.

To create the database.

Create MainAbilitySlice.java ability and Add the below code.

Create ability_main.xml layout and add the below code.

Create User.java class and add the below code.

Create InsertDataSlice.java class and add the below code.

Create ability_query_data.xml layout and add the below code.

Create ReadDataDBAbilitySlice.java ability and add the below code.

Create ORMItemProvider.java ability and add the below code.

Create ability_read_data.xml layout and add the below code.

6. To build apk and run in device, choose Build > Generate Key and CSR Build for Hap(s)\ APP(s) or Build and Run into connected device, follow the steps.

Result

  1. Click on UI ‘Store Data in DB’ button. It will navigate to Store data screen as per below images.

2. Enter data and click on “Save data” button. It will store data in RDB as per below images.

Pros: It is easy to use, store data in local memory without internet connection.

Cons: The database uses tables having rows and columns which consumes a lot of physical memory and it becomes a disadvantage of the database. Do not forgot to call flush method to store data.

3. Click on “Fetch Data DB” button, we will read data from RDB as per below images.

Pros: It is easy to use, fetch data from local memory without internet connection. Base on multiple condition, we can fetch data as per user requirement.

Cons: It will works locally, a single user can access.

4. Click on “Delete Data DB” button, we will read data from RDB as per below images.

Pros: We can delete database easily and clear consumed memory.

Cons: After deleting data, it is very difficult to retrieve same data.

Tips and Tricks

  • Always use the latest version of DevEcho Studio.
  • Use Harmony Device Simulator from HVD section.
  • Do not forgot to call flush method after update, delete and insert.

Conclusion

In this article, we have learnt Object Relational Mapping database (ORM) in Harmony OS. The ORM database scenario can be used when data can be divided into one or more objects and we do not want to use complex SQL statements to operate the data, including to add, delete, modify, and query commands to manipulate data.

Thanks for reading the article, please do like and comment your queries or suggestions.

References

Harmony OS: https://www.harmonyos.com/en/develop/?ha_source=hms1

Object Relational Mapping Database:

https://developer.harmonyos.com/en/docs/documentation/doc-guides/database-orm-overview-0000000000030070?ha_source=hms1

--

--

No responses yet