JDX Programming Code Snippets

Simple Mapping 1

This snippet shows how to define default mapping for a POJO (Plain Old Java Object) class. It also shows JDX ORM programming code of how instances of such a class can easily be inserted, deleted, updated, and retrieved in an intuitive object oriented way.

We use a simple Employee class for illustration.

Class Definitions

  1. package com.softwaretree.jdxandroidsimpleexample.model;
  3. import java.util.Date;
  5. public class Employee {
  6. private int id;
  7. private String name;
  8. private Date DOB;
  9. private boolean exempt;
  10. private float compensation;
  12. /**
  13. * Default no-arg constructor needed for JDX
  14. */
  15. public Employee() {
  16. }
  18. // Other constructors and accessor (setter/getter) methods omitted
  19. }
  • A simple POJO (Plain Old Java Object) class with five attributes of various types
    (line 5)

Mapping Specification

  1. CLASS com.softwaretree.jdxandroidsimpleexample.model.Employee
  3. ;
  • This is a very simple and minimal mapping specification for a class.
  • The com.softwaretree.jdxandroidsimpleexample.model.Employee class is mapped to a table with a default name of 'Employee'.
    (line 1)
  • The 'id' attribute serves as the primary key of the Employee objects.
    (line 2)
  • All the attributes are automatically mapped to similarly named columns by default. The data type of each column is considered to be whatever the default column type is for the Java type of the corresponding attribute. For example, the 'id' column will be of type 'INTEGER' and the 'name' column will be of type 'TEXT' in the database table 'Employee'.

Programming Example

  1. // Assuming JDXSetup of type JDXSetup is already initialized
  2. JDXHelper jdxHelper = new JDXHelper(jdxSetup);
  3. String employeeClassName = Employee.class.getName();
  5. // First delete all existing employees from the database.
  6. jdxHelper.delete2(employeeClassName, null);
  8. // Create and save a new employee Mark
  9. Employee emp = new Employee(1, "Mark", …);
  10. jdxHelper.insert(emp, false);
  12. // Create and save a new employee Bill
  13. emp = new Employee(2, "Bill", …);
  14. jdxHelper.insert(emp, false);
  16. // Retrieve all the employees
  17. List employees = jdxHelper.getObjects(employeeClassName, null);
  19. // Retrieve employee Bill (id=2)
  20. emp = (Employee) jdxHelper.getObjectById(employeeClassName, "id=2", …);
  22. // Change and update attributes of Bill
  23. emp.setExempt(true);
  24. emp.setCompensation((float) 103003);
  25. jdxHelper.update(emp, false);
  • JDXHelper classt presents a simplified interface to JDX ORM.
    (line 2)
  • Bulk delete of objects from the database can be done without first retrieving them.
    (line 6)
  • POJO object can easily be persisted in the database using JDX.
    (lines 10, 14)
  • A collection of objects can be retrieved using their class name and a predicate. A null predicate means retrieve all objects.
    (line 17)
  • An object can be retrieved using its object id (primary key).
    (line 20)
  • An object can be updated in memory using POJO setter methods and can easily be updated in the database using JDX.
    (lines 23, 24, 25)