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.
- package com.softwaretree.jdxandroidsimpleexample.model;
- import java.util.Date;
- public class Employee {
- private int id;
- private String name;
- private Date DOB;
- private boolean exempt;
- private float compensation;
- /**
- * Default no-arg constructor needed for JDX
- */
- public Employee() {
- }
- // Other constructors and accessor (setter/getter) methods omitted
- }
- CLASS com.softwaretree.jdxandroidsimpleexample.model.Employee
- PRIMARY_KEY id
- ;
- // Assuming JDXSetup of type JDXSetup is already initialized
- JDXHelper jdxHelper = new JDXHelper(jdxSetup);
- String employeeClassName = Employee.class.getName();
- // First delete all existing employees from the database.
- jdxHelper.delete2(employeeClassName, null);
- // Create and save a new employee Mark
- Employee emp = new Employee(1, "Mark", …);
- jdxHelper.insert(emp, false);
- // Create and save a new employee Bill
- emp = new Employee(2, "Bill", …);
- jdxHelper.insert(emp, false);
- // Retrieve all the employees
- List employees = jdxHelper.getObjects(employeeClassName, null);
- // Retrieve employee Bill (id=2)
- emp = (Employee) jdxHelper.getObjectById(employeeClassName, "id=2", …);
- // Change and update attributes of Bill
- emp.setExempt(true);
- emp.setCompensation((float) 103003);
- jdxHelper.update(emp, false);