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.
- 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;
- public class SimpleAddr {
- private String addrId;
- private String addr1;
- private String addr2;
- private String city;
- private String state;
- private String zip;
- public SimpleAddr() {
- }
- // Other constructors and accessor (setter/getter) methods omitted
- package com.softwaretree.jdxrelationshipsexample.model;
- public class SimpleEmp {
- private String empId;
- private String empName;
- private String title;
- private float salary;
- private int deptId;
- private SimpleDept dept; // identified by deptId
- private SimpleAddr address; // identified by empId
- public SimpleEmp() {
- }
- // Other constructors and accessor (setter/getter) methods omitted
- JDX_OBJECT_MODEL_PACKAGE com.softwaretree.jdxandroidrelationshipsexample.model
- CLASS .SimpleDept TABLE Simple_Dept
- PRIMARY_KEY deptId
- ;
- CLASS .SimpleAddr TABLE Simple_Address
- IMPLICIT_ATTRIB addrId ATTRIB_TYPE java.lang.String
- PRIMARY_KEY addrId
- SQLMAP FOR addr2 NULLABLE
- ;
- CLASS .SimpleEmp TABLE Simple_Employee
- PRIMARY_KEY empId
- RELATIONSHIP dept REFERENCES .SimpleDept WITH deptId
- RELATIONSHIP address REFERENCES .SimpleAddr BYVALUE WITH empId
- ;
- // Assuming JDXSetup of type JDXSetup is already initialized
- // Obtain ORM handles
- JXResource jxResource = jdxSetup.checkoutJXResource();
- JXSession jxSessionHandle = jxResource.getJXSessionHandle();
- JDXS jdxHandle = jxResource.getJDXHandle();
- String departmentClassName = SimpleDept.class.getName();
- String employeeClassName = SimpleEmp.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 all the existing SimpleEmp objects along with the associated
- // SimpleAddr objects.
- jdxHandle.delete2(employeeClassName, null, JDXS.FLAG_DEEP);
- // Now delete an existing SimpleDept object. Shallow or Deep does not matter.
- jdxHandle.delete2(departmentClassName, “deptId=1”, JDXS.FLAG_SHALLOW);
- // Now create and insert a new SimpleDept object. Shallow or Deep does not matter.
- String companyId = "C1";
- int deptId1 = 1;
- SimpleDept dept1 = new SimpleDept(deptId1, companyId, "Department 1");
- jdxHandle.insert(dept1, JDXS.FLAG_SHALLOW, null);
- // Create and insert a SimpleEmp object along with the
- // associated SimpleAddr object with one JDX call.
- SimpleAddr addr1 = new SimpleAddr(…);
- SimpleEmp emp1 = new SimpleEmp(empId1, …, deptId1, dept1, addr1);
- jdxHandle.insert(emp1, JDXS.FLAG_DEEP, null);
- // Commit the transaction in the database. jxSessionHandle.tx_commit();
- // Retrieve all the SimpleEmp objects with a shallow query
- // (no related SimpleDept and SimpleAddr objects would be fetched).
- List queryResults = jdxHandle.query(employeeClassName, null, JDXS.ALL, JDXS.FLAG_SHALLOW, null);
- // Retrieve all the SimpleEmp objects with a deep query.
- // The related SimpleDept and SimpleAddr objects would also be fetched.
- queryResults = jdxHandle.query(employeeClassName, null, JDXS.ALL, JDXS.FLAG_DEEP, null);
- } catch (Exception ex) {
- throw ex;
- } finally {
- jdxSetup.checkinJXResource(jxResource);
- }