JDX Programming Code Snippets

Simple Mapping 2


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.

Class Definitions

                
                
  1. package com.softwaretree.jdxandroidsimpleexample.model;
  2.  
  3. import java.util.Date;
  4.  
  5. public class Employee {
  6. private int id;
  7. private String name;
  8. private Date DOB;
  9. private boolean exempt;
  10. private float compensation;
  11. private transient int calc; // A transient (non-persistent) attribute
  12. private int waitingNumber; // Current waiting list number; need not be persisted
  13.  
  14. /**
  15. * Default no-arg constructor needed for JDX
  16. */
  17. public Employee() {
  18. }
  19.  
  20. // Other constructors and accessor (setter/getter) methods omitted
  21. }
Notes:
  • A simple POJO (Plain Old Java Object) class with five attributes of various types.
    (line 5)
  • The 'calc’ attribute would not be persisted by JDX because it is declared transient
    (line 11)
  • The waitingNumber attribute holds some transitory information that need not be persisted across different sessions (invocations) of the application
    (line 12)

Mapping Specification

           
           
  1. CLASS com.softwaretree.jdxandroidsimpleexample.model.Employee TABLE EMPLOYEE
  2. PRIMARY_KEY id
  3. IGNORE waitingNumber
  4. SQLMAP FOR compensation COLUMN_NAME salary
  5. ;
Notes:
  • The com.softwaretree.jdxandroidsimpleexample.model.Employee class is mapped to a table named ‘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 except for the following:
    • The waitingNumber attribute is not persisted at all because of the IGNORE specification.
      (line 3)
    • The name of the column storing compensation information would be ‘salary’ because of the SQLMAP specification.
      (line 4)

Programming Example

                
                
  1. // Assuming JDXSetup of type JDXSetup is already initialized
  2. JDXHelper jdxHelper = new JDXHelper(jdxSetup);
  3. String employeeClassName = Employee.class.getName();
  4.  
  5. // First delete all existing employees from the database.
  6. jdxHelper.delete2(employeeClassName, null);
  7.  
  8. // Create and save a new employee Mark
  9. Employee emp = new Employee(1, "Mark", …);
  10. jdxHelper.insert(emp, false);
  11.  
  12. // Create and save a new employee Bill
  13. emp = new Employee(2, "Bill", …);
  14. jdxHelper.insert(emp, false);
  15.  
  16. // Retrieve all the employees
  17. List employees = jdxHelper.getObjects(employeeClassName, null);
  18.  
  19. // Retrieve employee Bill (id=2)
  20. emp = (Employee) jdxHelper.getObjectById(employeeClassName, "id=2", …);
  21.  
  22. // Change and update attributes of Bill
  23. emp.setExempt(true);
  24. emp.setCompensation((float) 103003);
  25. jdxHelper.update(emp, false);
Notes:
  • All the notes of the Simple Mapping 1 Programming Example apply.
  • There is no need to change the ORM related code at all to take care of non-default column names, a transient attribute, or ignoring an attribute for persistence.