JDX Programming Code Snippets

Mapping One-To-One Relationships


This snippet shows how to define one-to-one relationships. It also shows example of deep and shallow operations involving related objects.

In the example object mode, an employee has an address and works in a department.

Class Definitions

                
                
  1. package com.softwaretree.jdxrelationshipsexample.model;
  2.  
  3. public class SimpleDept {
  4.  
  5. private int deptId;
  6. private String companyId;
  7. private String deptName;
  8.  
  9. public SimpleDept() {
  10. }
  11. // Other constructors and accessor (setter/getter) methods omitted
Notes:
  • A simple POJO (Plain Old Java Object) class with attributes of various types for a department.
    (line 3)
                
                
  1. package com.softwaretree.jdxrelationshipsexample.model;
  2.  
  3. public class SimpleAddr {
  4. private String addrId;
  5. private String addr1;
  6. private String addr2;
  7. private String city;
  8. private String state;
  9. private String zip;
  10.  
  11. public SimpleAddr() {
  12. }
  13. // Other constructors and accessor (setter/getter) methods omitted
Notes:
  • A simple POJO (Plain Old Java Object) class with attributes of various types for an address.
    (line 3)
  • The addrId attribute should be implicitly updated from the PK value of the containing SimpleEmp object.
    (line 4)
                
                
  1. package com.softwaretree.jdxrelationshipsexample.model;
  2.  
  3. public class SimpleEmp {
  4. private String empId;
  5. private String empName;
  6. private String title;
  7. private float salary;
  8. private int deptId;
  9. private SimpleDept dept; // identified by deptId
  10. private SimpleAddr address; // identified by empId
  11.  
  12. public SimpleEmp() {
  13. }
  14. // Other constructors and accessor (setter/getter) methods omitted
Notes:
  • A POJO (Plain Old Java Object) class with attributes of various types for an employee.
    (line 3)
  • The dept attribute references a SimpleDept object in a one-to-one relationship. We want this relationship to be of type BYREFERENCE, which means that the referenced SimpleDept object would *not* be inserted, updated, or deleted with a referencing SimpleEmp object.
    (line 9)
  • The address attribute references a SimpleAddr object in a one-to-one relationship. We want this relationship to be of type BYVALUE, which means that the referenced SimpleAddr object will be inserted, updated, or deleted with a referencing SimpleEmp object.
    (line 10)

Mapping Specification

           
           
  1. JDX_OBJECT_MODEL_PACKAGE com.softwaretree.jdxandroidrelationshipsexample.model
  2.  
  3. CLASS .SimpleDept TABLE Simple_Dept
  4. PRIMARY_KEY deptId
  5. ;
  6.  
  7. CLASS .SimpleAddr TABLE Simple_Address
  8. IMPLICIT_ATTRIB addrId ATTRIB_TYPE java.lang.String
  9. PRIMARY_KEY addrId
  10. SQLMAP FOR addr2 NULLABLE
  11. ;
  12. CLASS .SimpleEmp TABLE Simple_Employee
  13. PRIMARY_KEY empId
  14. RELATIONSHIP dept REFERENCES .SimpleDept WITH deptId
  15. RELATIONSHIP address REFERENCES .SimpleAddr BYVALUE WITH empId
  16. ;
Notes:
  • JDX_OBJECT_MODEL_PACKAGE specifies the default package name for all the classes whose names are specified starting with a dot (.). This is to facilitate abbreviating the class names in the mapping specification.
    (line 1)
  • Non-default table names have been specified for each class.
    (line 3, 3, 13)
  • In the mapping for the SimpleAddr class:
    • The addrId attribute is declared IMPLICT_ATTRIB, which means that if an object of this class is referenced by another object (BYVALUE), the other object’s primary key attribute would automatically be used by JDX to initialize this object’s addrId attribute.
      (line 8)
    • The addr2 attribute is nullable, which implies that the corresponding column in the database should be NULLable.
      (line 10)
  • In the mapping specification for the SimpleEmp class:
    • The dept attribute references a SimpleDept object by reference (BYREFERENCE is the default, not BYVALUE). This means that the referenced SimpleDept object will *not* be inserted, updated, or deleted with the referencing SimpleEmp object.
      (line 15)
    • The address attribute references a SimpleAddr object by value. This means that, by default, the referenced SimpleAddr object will be inserted, updated, or deleted with a referencing SimpleEmp object.
      (line 16)

Programming Example

                
                
  1. // Assuming JDXSetup of type JDXSetup is already initialized
  2.  
  3. // Obtain ORM handles
  4. JXResource jxResource = jdxSetup.checkoutJXResource();
  5. JXSession jxSessionHandle = jxResource.getJXSessionHandle();
  6. JDXS jdxHandle = jxResource.getJDXHandle();
  7.  
  8. String departmentClassName = SimpleDept.class.getName();
  9. String employeeClassName = SimpleEmp.class.getName();
  10. try {
  11. // Start a new transaction to make all the following statements
  12. // (until tx_commit) to execute as one unit of operation.
  13. jxSessionHandle.tx_begin();
  14.  
  15. // First delete all the existing SimpleEmp objects along with the associated
  16. // SimpleAddr objects.
  17. jdxHandle.delete2(employeeClassName, null, JDXS.FLAG_DEEP);
  18.  
  19. // Now delete an existing SimpleDept object. Shallow or Deep does not matter.
  20. jdxHandle.delete2(departmentClassName, “deptId=1”, JDXS.FLAG_SHALLOW);
  21.  
  22. // Now create and insert a new SimpleDept object. Shallow or Deep does not matter.
  23. String companyId = "C1";
  24. int deptId1 = 1;
  25.  
  26. SimpleDept dept1 = new SimpleDept(deptId1, companyId, "Department 1");
  27. jdxHandle.insert(dept1, JDXS.FLAG_SHALLOW, null);
  28. // Create and insert a SimpleEmp object along with the
  29. // associated SimpleAddr object with one JDX call.
  30. SimpleAddr addr1 = new SimpleAddr(…);
  31. SimpleEmp emp1 = new SimpleEmp(empId1, …, deptId1, dept1, addr1);
  32. jdxHandle.insert(emp1, JDXS.FLAG_DEEP, null);
  33. // Commit the transaction in the database. jxSessionHandle.tx_commit();
  34. // Retrieve all the SimpleEmp objects with a shallow query
  35. // (no related SimpleDept and SimpleAddr objects would be fetched).
  36. List queryResults = jdxHandle.query(employeeClassName, null, JDXS.ALL, JDXS.FLAG_SHALLOW, null);
  37. // Retrieve all the SimpleEmp objects with a deep query.
  38. // The related SimpleDept and SimpleAddr objects would also be fetched.
  39. queryResults = jdxHandle.query(employeeClassName, null, JDXS.ALL, JDXS.FLAG_DEEP, null);
  40. } catch (Exception ex) {
  41. throw ex;
  42. } finally {
  43. jdxSetup.checkinJXResource(jxResource);
  44. }
Notes:
  • All the notes of the Simple Mapping 1 Programming 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
    • invoke multiple operations in one transaction and
    • to interact more directly with the ORM system.
    (lines 4, 5, 6, 13)
  • As various comments in the above programming code suggest, objects with relationships can easily be manipulated using JDX APIs in a shallow or deep fashion.
    (lines 17, 20, 27, 32, 36, 39)
  • It is also possible to do operations which are neither shallow, nor deep, but something in-between (directed operations). The JDX user manual has more details on directed operations.