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.
- package com.softwaretree.jdxrelationshipsexample.model;
- public class SimpleDept {
- private int deptId;
- private String companyId;
- private String deptName;
- public SimpleDept() {
- }
- // Other constructors and accessor (setter/getter) methods omitted
- package com.softwaretree.jdxrelationshipsexample.model;
- import java.util.List;
- public class SimpleCompany {
- private String companyId;
- private String companyName;
- private String city;
- private String state;
- public List<SimpleDept> depts; // identified by companyId
- public SimpleCompany() {
- }
- // Other constructors and accessor (setter/getter) methods omitted
- JDX_OBJECT_MODEL_PACKAGE com.softwaretree.jdxandroidrelationshipsexample.model
- CLASS .SimpleDept TABLE Simple_Dept
- PRIMARY_KEY deptId
- ;
- COLLECTION_CLASS CollectionDept COLLECTION_TYPE JAVACOLLECTION ELEMENT_CLASS .SimpleDept
- PRIMARY_KEY companyId
- ORDERBY_DESC deptId
- ;
- CLASS .SimpleCompany TABLE Simple_Company
- PRIMARY_KEY companyId
- RELATIONSHIP depts REFERENCES CollectionDept BYVALUE WITH companyId
- ;
- // Assuming JDXSetup of type JDXSetup is already initialized
- // Obtain ORM handles
- JXResource jxResource = jdxSetup.checkoutJXResource();
- JXSession jxSessionHandle = jxResource.getJXSessionHandle();
- JDXS jdxHandle = jxResource.getJDXHandle();
- String companyClassName = SimpleCompany.class.getName();
- try {
- // Start a new transaction to make all the following statements
- // (until tx_commit) to execute as one unit of operation
- jxSessionHandle.tx_begin();
- // First delete existing SimpleCompany and associated SimpleDept objects
- jdxHandle.delete2(companyClassName, null, JDXS.FLAG_DEEP);
- // Now create a new SimpleCompany object and the associated SimpleDept objects
- String companyId = "C1";
- company1 = new SimpleCompany(companyId, "Corporation 1", …);
- List<SimpleDept> depts = new ArrayList<SimpleDept>();
- dept1 = new SimpleDept(deptId1, companyId, "Department 1");
- depts.add(dept1);
- dept2 = new SimpleDept(deptId2, companyId, "Department 2");
- depts.add(dept2);
- company1.setDepts(depts);
- // Now insert the newly created SimpleCompany along with the
- // associated SimpleDept objects with one JDX call
- jdxHandle.insert(company1, JDXS.FLAG_DEEP, null);
- // Commit the transaction
- jxSessionHandle.tx_commit();
- // Retrieve all SimpleCompany objects along with their departments
- // with a deep query
- List queryResults = jdxHandle.query(companyClassName, null, JDXS.ALL, JDXS.FLAG_DEEP, null);
- // Retrieve all SimpleCompany (top-level) objects without their departments
- // with a shallow query
- List queryResults = jdxHandle.query(companyClassName, null, JDXS.ALL, JDXS.FLAG_SHALLOW, null);
- } catch (Exception ex) {
- throw ex;
- } finally {
- jdxSetup.checkinJXResource(jxResource);
- }