JDX Programming Code Snippets

Mapping Auto-Increment Columns for Primary Key

This snippet shows how to define and use auto-increment columns for generating primary keys for objects of a class. JDX does not set values of the corresponding columns through the INSERT and UPDATE statements used for storing or updating the instances of this class. However, the query operations do fetch the corresponding column values.

JDX also provides a named sequence generator facility that can alternatively be used to efficiently generate unique ids that you can assign to your objects before saving them in the database.

Class Definitions

  1. package com.softwaretree.jdxandroidautoincrementexample.model;
  2. import java.util.Date;
  3. public class Employee {
  4. private int id;
  5. private String name;
  6. private Date DOB;
  7. private boolean exempt;
  8. private float compensation;
  9. /**
  10. *Default no-arg constructor needed for JDX
  11. */
  12. public Employee() {
  13. }
  14. // id field is generated by the database.
  15. public Employee(String name, Date DOB, boolean exempt, float compensation) {
  16. this.name = name;
  17. this.DOB = DOB;
  18. this.exempt = exempt;
  19. this.compensation = compensation;
  20. }
  22. // Other constructors and accessor (setter/getter) methods omitted
  • A simple POJO (Plain Old Java Object) class with five attributes of various types.
    (line 5)
  • We want the value of the id attribute to be automatically generated in the database using an auto-increment column.
    (line 6)
  • This constructor does not take a parameter for the value of the id attribute; its value will automatically be generated by the database.
    (lines 8 and 12)

Mapping Specification

  1. CLASS com.softwaretree.jdxandroidautoincrementexample.model.Employee TABLE Employee
  5. ;
  • The id attribute serves as the primary key of the Employee objects.
    (line 2)
  • A SQLTYPE specification of 'INTEGER PRIMARY KEY AUTOINCREMENT' for the id attribute is used to create an auto-increment column in the underlying table (Employee) of the SQLite database.
    (line 3)
  • The RDBMS_GENERATED specification means that the values of the specified attributes (e.g., id) will automatically be generated by the underlying database; JDX should not set values of the corresponding columns through the INSERT and UPDATE statements used for storing or updating the instances of the mapped class (e.g., Employee).
    (line 4)

Programming Example

  1. // Assuming jdxSetup of type JDXSetup has already been initialized.
  2. // Obtain ORM handles.
  3. JXResource jxResource = jdxSetup.checkoutJXResource();
  4. JXSession jxSessionHandle = jxResource.getJXSessionHandle();
  5. JDXS jdxHandle = jxResource.getJDXHandle();
  6. String employeeClassName = Employee.class.getName();
  7. DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
  8. dfm.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
  9. try {
  10. // Create and save a new employee Mark
  11. Employee emp = new Employee("Mark", dfm.parse("1981-01-01"), true, (float) 51001);
  12. jdxHandle.insert(emp, 0, null);
  13. // Create and save a new employee Bill
  14. emp = new Employee("Bill", dfm.parse("1982-02-02"), false, (float) 52002);
  15. jdxHandle.insert(emp, 0,null);
  16. // Retrieve all the employees
  17. List queryResults = jdxHandle.query(employeeClassName, null, JDXS.ALL, JDXS.FLAG_SHALLOW, null);
  18. // Retrieve employee Bill (name='Bill')
  19. queryResults = jdxHandle.query(employeeClassName, "name='Bill'", 1, JDXS.FLAG_SHALLOW, null);
  20. if (queryResults.size() == 1) {
  21. emp = (Employee) queryResults.get(0);
  22. // Change and update attributes of Bill
  23. emp.setExempt(true);
  24. emp.setCompensation((float) 52002.02);
  25. jdxHandle.update(emp, 0, null);
  26. }
  27. } catch (Exception ex) {
  28. Log.e("JDX", "Error", ex);
  29. throw ex;
  30. } finally {
  31. jdxSetup.checkinJXResource(jxResource);
  32. }
  • All the notes of the Simple Mapping 1 Example apply.
  • Whereas the Simple Mapping Example 1 uses a higher-level façade (JDXHelper) to interact with the JDX ORM system, this code snippet uses JXResource, JXSession, and JDXS objects to interact more directly with the ORM system.
    (lines 4, 5, 6)
  • The constructor of the Employee class does not initialize the id attribute. Its value is automatically generated in the database when the object is saved for the first time there.
    (lines 14, 15, 18, 19)
  • A JDX query retrieves the auto-generated values of the id attribute of the qualifying objects.
    (lines 22, 25)
  • A JDX update operation would not change the value of the auto-generated column for the id attribute.
    (line 31)