Introduction:
ORMLite (Object Relational Mapping) database provides lightweight data mapping between Java Classes & SQL Databases. ORMLite mainly supports the following JDBC connections: MySQL, Postgres, H2, SQLite, Derby, HSQLDB, Microsoft SQL Server and so forth.
Since it is not an in-built library in Android like SQLite, developers need to add the respective Jar file or dependency first before starting development.
Why ORMLite?
Before delving into the topic of ORMLite, we need to understand why we need ORMLite. As compared to SQLite database in an Android library, ORMLite can be used repeatedly without making too many changes to the code (in terms of queries).While dealing with ORMLite, it is not necessary to learn SQL as another language since we can use objects and pre-defined methods to create our database.
How to start & use ORMLite:
To start development using ORMLite in Android, we need to follow the below mentioned steps:
Downloading ORMLite jar file (Android):
For using ORMLite with Android application, you should download ormlite-android-4.43.jar and ormlite-core-4.43.jar & above version files. Add them as library files to the project and you are ready to start the development.
Configuration of your model class:
After successful download of ORMLite jar files, the second step for a user is to configure their Model class. Users can use the following annotations to configure their classes.
- @DatabaseTable: The @DatabaseTable annotation is used to provide a specific user-define name to a database while creating a Database Table..
- @DatabaseField: The @DatabaseField annotations are used to provide fields to the database created.
- @DatabaseField(id=true): The @DatabaseField annotation is used if a user need to declare any of the fields created in the database as a Primary Key.
Below is an example to demonstrate ORMLite model class:
Example:
@DatabaseTable(tableName = "employee") public class Employee{ @DatabaseField(id = true) private String name; @DatabaseField private String empID; @DatabaseField Private double salary; public Employee() { // ORMLite needs a no-arg constructor with package visibility. } public Employee(String name, String empID,String salary) { this.name = name; this.empID = empID; this.salary =salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getempID() { return empID; } public void setempID(String empID) { this.empID= empID; } Public double getsalary(){ return salary; } public void setsalary(double salary) { this.salary= salary; } }
Configuration of DAO:
DAO stands for Data Access Object which provides us CRUD (Create, Read, Update & Delete) database operations. The simplest way to create a DAO is to use a static createDao method in the DaoManager class. The DAO of Employee class defined will look like:
**Private Dao<Employee,String> empdao = DaoManager.create(connectionSource,Employee.class);
Custom Statement Builder:
DAO has some pre-defined methods to query for an object that matches an ID field as (queryForId) & if it matches all objects, then (queryForAll) and some other simple matching methods. However, for more custom queries we have queryBuilder() method which returns a QueryBuilder object for a DAO with which you can construct custom queries of your database tables.
Query Builder Basics:
To start working with query builder, we have to first provide column names to our database fields so that it will be easy for us to use them while building our query. Below is an example to show how to build a query in ORMLite:
@DatabaseTable(tableName =“Employee") public class Employee{ public static final String FIELD_NAME = "name"; ... @DatabaseField(canBeNull = false, columnName = FIELD_NAME) //this will check that the name given by user to column FIELD_NAME should not be empty private String name; … Now let us start building query as: QueryBuilder<Employee, String> queryBuilder = empdao .queryBuilder(); Where<Employee, String> where = queryBuilder.where(); where.eq(Employee.FIELD_NAME, "Rohit"); where.and(); where.eq(Employee.FIELD_Emp_ID, "12345"); PreparedQuery<Employee> preparedQuery = queryBuilder.prepare();
Using this With Android OS:
While working with Android operating system we need to create our own database helper class extends to OrmLiteSqliteOpenHelper class. This class creates an override method as onCreate and onUpgrade .These override methods create and upgrade your database when your application is going to start and can also provide the DAO.
Classes used by other classes:
While working with this, you should keep your helper class as public so that all other classes can easily access it. Once you have defined your database helper you need to use it in your activities.
The simple method to use your OpenHelperManager is to extend it to OrmLiteBaseActivity for each of your activities. If you don’t want to follow the above method you can directly call OpenHelperManager.gethelper() method to your respective classes.
Once you use OpenHelperManager.gethelper() save the helper class and then call OpenHelperManager.release() to release the call.
Table Config File:
We have used ORMLite annotations for configuring our class. If required you can also remove the annotations which makeour data access objects operations a bit faster.. In ORMLite you can configure data using a text configuration file. This configuration file consists of different tags related to database table and the fields associated with it. The OrmLiteConfigUtil class writes this config text file in android raw resource folder and can be declared in your database helper class. Below is an example of config text file:
public class DatabaseConfigUtil extends OrmLiteConfigUtil { public static void main(String[] args) throws Exception { writeConfigFile("ormlite_config.txt"); } }