Similar to Simple Mapping 1, this snippet additionally shows how to specify a non-default table name, how to specify the table column names to be different than the class attribute names, and how to ignore some attributes for persistence.
- 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;
- private transient int calc; // A transient (non-persistent) attribute
- private int waitingNumber; // Current waiting list number; need not be persisted
- /**
- * Default no-arg constructor needed for JDX
- */
- public Employee() {
- }
- // Other constructors and accessor (setter/getter) methods omitted
- }
- CLASS com.softwaretree.jdxandroidsimpleexample.model.Employee TABLE EMPLOYEE
- PRIMARY_KEY id
- IGNORE waitingNumber
- SQLMAP FOR compensation COLUMN_NAME salary
- ;
- // 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);