JDX Programming Code Snippets

Mapping One-To-Many Relationships


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

In the example class model, a company has many departments.

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
                
                
  1. package com.softwaretree.jdxrelationshipsexample.model;
  2.  
  3. import java.util.List;
  4.  
  5. public class SimpleCompany {
  6.  
  7. private String companyId;
  8. private String companyName;
  9. private String city;
  10. private String state;
  11. public List<SimpleDept> depts; // identified by companyId
  12.  
  13. public SimpleCompany() {
  14. }
  15. // Other constructors and accessor (setter/getter) methods omitted
Notes:
  • A POJO (Plain Old Java Object) class with attributes of various types for a company.
    (line 5)
  • The depts attribute references a collection (ArrayList) of SimpleDept objects in a one-to-many relationship. We want this relationship to be of type BYVALUE, which means that, by default, the referenced collection of SimpleDept objects will be inserted, updated, or deleted with a referencing SimpleCompany object.
    (line 11)

Mapping Specification

              
              
  1. JDX_OBJECT_MODEL_PACKAGE com.softwaretree.jdxandroidrelationshipsexample.model
  2. CLASS .SimpleDept TABLE Simple_Dept
  3. PRIMARY_KEY deptId
  4. ;
  5. COLLECTION_CLASS CollectionDept COLLECTION_TYPE JAVACOLLECTION ELEMENT_CLASS .SimpleDept
  6. PRIMARY_KEY companyId
  7. ORDERBY_DESC deptId
  8. ;
  9. CLASS .SimpleCompany TABLE Simple_Company
  10. PRIMARY_KEY companyId
  11. RELATIONSHIP depts REFERENCES CollectionDept BYVALUE WITH companyId
  12. ;
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 Object-Relational Mapping specification.
    (line 1)
  • Non-default table names have been specified for each class.
    (lines 3, 12)
  • The COLLECTION_CLASS specification for CollectionDept defines a collection of SimpleDept objects.
    (lines 7)
  • The SimpleDept objects belonging to a collection have the same value for the (PRIMARY_KEY) attribute companyId.
    (lines 8)
  • The collection of SimpleDept will be retrieved in the order of decreasing value of deptId (ORDERBY_DESC).
    (lines 9)
  • In the mapping specification for the SimpleCompany class, depts attribute references the previously defined CollectionDept by value (BYVALUE). This means that, by default, the referenced collection of SimpleDept objects will be inserted, updated, or deleted with the referencing SimpleCompany object.
    (lines 14)

Programming Example

                
                
  1. // Assuming JDXSetup of type JDXSetup is already initialized
  2. // Obtain ORM handles
  3. JXResource jxResource = jdxSetup.checkoutJXResource();
  4. JXSession jxSessionHandle = jxResource.getJXSessionHandle();
  5. JDXS jdxHandle = jxResource.getJDXHandle();
  6. String companyClassName = SimpleCompany.class.getName();
  7. try {
  8. // Start a new transaction to make all the following statements
  9. // (until tx_commit) to execute as one unit of operation
  10. jxSessionHandle.tx_begin();
  11. // First delete existing SimpleCompany and associated SimpleDept objects
  12. jdxHandle.delete2(companyClassName, null, JDXS.FLAG_DEEP);
  13. // Now create a new SimpleCompany object and the associated SimpleDept objects
  14. String companyId = "C1";
  15. company1 = new SimpleCompany(companyId, "Corporation 1", …);
  16. List<SimpleDept> depts = new ArrayList<SimpleDept>();
  17. dept1 = new SimpleDept(deptId1, companyId, "Department 1");
  18. depts.add(dept1);
  19. dept2 = new SimpleDept(deptId2, companyId, "Department 2");
  20. depts.add(dept2);
  21. company1.setDepts(depts);
  22. // Now insert the newly created SimpleCompany along with the
  23. // associated SimpleDept objects with one JDX call
  24. jdxHandle.insert(company1, JDXS.FLAG_DEEP, null);
  25. // Commit the transaction
  26. jxSessionHandle.tx_commit();
  27. // Retrieve all SimpleCompany objects along with their departments
  28. // with a deep query
  29. List queryResults = jdxHandle.query(companyClassName, null, JDXS.ALL, JDXS.FLAG_DEEP, null);
  30. // Retrieve all SimpleCompany (top-level) objects without their departments
  31. // with a shallow query
  32. List queryResults = jdxHandle.query(companyClassName, null, JDXS.ALL, JDXS.FLAG_SHALLOW, null);
  33. } catch (Exception ex) {
  34. throw ex;
  35. } finally {
  36. jdxSetup.checkinJXResource(jxResource);
  37. }
Notes:
  • All the notes of the Simple Mapping 1 and Mapping One-To-One Relationships Programming Examples 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
    (lines 4, 5, 6, 13)
    • invoke multiple operations in one transaction and
    • to interact more directly with the ORM system.
  • 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 16, 31, 38, 42)