Skip to content

SoftwareTree/JDX_ClassHierarchyExample

Repository files navigation

Note: This file is written in Markdown and is best viewed with a Markdown viewer (e.g., GitHub, GitLab, VS Code, or a dedicated Markdown reader). Viewing it in a plain text editor may not render the formatting as intended.

Copyright (c) 2026 Software Tree

JDX_ClassHierarchyExample

Overview

This project demonstrates how JDX ORM supports Java class inheritance (class hierarchy), allowing objects of a superclass and all its subclasses to be persisted, queried, and deleted through the database in a polymorphic way.

The domain model is a hierarchy rooted at Person:

Person
├── Intern
└── BaseEmployee
    ├── PermEmployee
    └── TempEmployee

Key inheritance behaviors demonstrated:

  • A query or deep delete on a superclass (e.g., Person) automatically includes all objects of its subclasses.
  • A query or deep delete on BaseEmployee includes PermEmployee and TempEmployee objects.
  • Subclass-specific queries with predicates (e.g., all BaseEmployee objects in the Engg department) also correctly span subclasses.

Unique IDs across the entire hierarchy are generated using a single shared PersonIdSequence named sequence, ensuring no ID collisions between objects of different subclasses.


Prerequisites

  • Java JDK 8 or higher installed and on the system PATH.
  • JDX ORM SDK installed. Set the environment variable JX_HOME to the SDK's top-level installation directory.
  • A supported JDBC-compatible database (SQLite is pre-configured; a MySQL example is also included in the .jdx file).

Project Structure

JDX_ClassHierarchyExample/
├── config/
│   └── classhierarchy_example.jdx   # ORM mapping specification file
├── src/
│   └── com/softwaretree/jdxclasshierarchyexample/
│       ├── ClassHierarchyExample.java  # Main application entry point
│       └── model/
│           ├── Person.java             # Root superclass
│           ├── BaseEmployee.java       # Subclass of Person
│           ├── Intern.java             # Subclass of Person
│           ├── PermEmployee.java       # Subclass of BaseEmployee
│           └── TempEmployee.java       # Subclass of BaseEmployee
├── bin/                                # Compiled .class files (generated)
├── sources.txt                         # List of Java source files for compilation
├── compile.cmd                         # Windows: compile the Java source files
├── compile.sh                          # Mac/Linux: compile the Java source files
├── setEnvironment.bat                  # Windows: sets classpath environment variable
├── setEnvironment.sh                   # Mac/Linux: sets classpath environment variable
├── runJDXExample.bat                   # Windows: run the sample application
├── runJDXExample.sh                    # Mac/Linux: run the sample application
├── forward.bat                         # Windows: create/recreate the database schema
├── forward.sh                          # Mac/Linux: create/recreate the database schema
├── JDXDemo.bat                         # Windows: launch the JDXDemo GUI application
├── JDXDemo.sh                          # Mac/Linux: launch the JDXDemo GUI application
└── README.md                           # This file

Domain Model

The class hierarchy and the fields introduced at each level are as follows:

Class Extends Table Own Fields
Person Person id (PK), name, DOB
Intern Person Intern school
BaseEmployee Person BaseEmployee dept
PermEmployee BaseEmployee PermEmployee salary
TempEmployee BaseEmployee TempEmployee hourlyRate

Each class is mapped to its own table. The id field (primary key) is shared across the hierarchy and assigned using the PersonIdSequence named sequence generator.


Key Components

config/classhierarchy_example.jdx — ORM Mapping File

This declarative file defines the class-to-table mappings and the shared sequence generator. Key elements:

  • JDX_DATABASE and JDBC_DRIVER — database connection and driver settings. Pre-configured for SQLite; a commented-out MySQL example is also included.
  • JDX_OBJECT_MODEL_PACKAGE — the base Java package, allowing short class name references within the file.
  • CLASS / PRIMARY_KEY — each class in the hierarchy is mapped individually to its own table, all sharing id as the primary key.
  • SEQUENCE PersonIdSequence — a single named sequence (MAX_INCREMENT 5, START_WITH 1) used to generate unique IDs across all classes in the hierarchy, preventing primary key collisions.

JDX ORM infers the inheritance relationships from the Java class definitions; no explicit inheritance declaration is required in the .jdx file.

Refer to the JDX Database & JDBC Driver Specification Guide for configuring other databases.

Note: Update JDX_DATABASE and JDBC_DRIVER to match your local database setup before running.


src/.../model/ — Domain Model Classes

Five POJOs forming the class hierarchy, each with a no-arg default constructor as required by JDX ORM:

  • Person — root class with id, name, and DOB (Date).
  • Intern — extends Person; adds school (String).
  • BaseEmployee — extends Person; adds dept (String).
  • PermEmployee — extends BaseEmployee; adds salary (int).
  • TempEmployee — extends BaseEmployee; adds hourlyRate (float).

src/.../ClassHierarchyExample.java — Main Application

The entry point of the sample application. It initializes JDX ORM and runs useJDXORM(), which demonstrates the following:

  1. Deep delete all existing Person objects (and all subclass objects) from the database, then verify counts across all classes are zero.
  2. Batch insert three Person objects (Barry, Larry, Mary); query and print all Person objects.
  3. Batch insert three Intern objects (Nina, Tommy, Elvis); query and print all Intern objects.
  4. Batch insert three BaseEmployee objects; query all Person objects — demonstrating that a Person query returns subclass instances too.
  5. Batch insert three PermEmployee objects; query PermEmployee objects, then BaseEmployee objects (which also returns PermEmployee instances).
  6. Batch insert two TempEmployee objects; run further polymorphic queries across the hierarchy.
  7. Filtered queries — e.g., all Person objects with name > 'Larry', and all BaseEmployee objects with dept = 'Engg'.
  8. Deep delete all BaseEmployee objects with dept = 'Engg' — cascades to matching PermEmployee and TempEmployee rows — then verify counts.
  9. Deep delete all remaining Person objects and verify all subclass tables are also cleared.

sources.txt — Source File List

Lists all .java source files to be compiled, one per line:

./src/com/softwaretree/jdxclasshierarchyexample/model/Person.java
./src/com/softwaretree/jdxclasshierarchyexample/model/BaseEmployee.java
./src/com/softwaretree/jdxclasshierarchyexample/model/Intern.java
./src/com/softwaretree/jdxclasshierarchyexample/model/PermEmployee.java
./src/com/softwaretree/jdxclasshierarchyexample/model/TempEmployee.java
./src/com/softwaretree/jdxclasshierarchyexample/ClassHierarchyExample.java

This file is passed to javac using the @sources.txt argument syntax.


compile.cmd / compile.sh — Compilation Scripts

Compiles all Java source files listed in sources.txt and outputs .class files into the bin/ directory.

  • Requires JX_HOME to be set to the JDX ORM SDK installation directory.
  • Links against jxclasses.jar (JDX ORM library) and json-20240303.jar (JSON support).
  • compile.cmd — Windows batch script (supports JDK 8; a commented line supports JDK 9+).
  • compile.sh — Mac/Linux shell script equivalent.

Windows:

compile.cmd

Mac/Linux:

chmod +x compile.sh   # first time only
./compile.sh

setEnvironment.bat / setEnvironment.sh — Environment Setup

Sets the CLASSPATH environment variable to include the JDX ORM libraries and the appropriate JDBC driver JAR for your database. Edit this file to point to the correct JDBC driver for your database before running the application.

  • setEnvironment.bat — Windows (uses ; as classpath separator).
  • setEnvironment.sh — Mac/Linux (uses : as classpath separator; sourced via source ./setEnvironment.sh).

runJDXExample.bat / runJDXExample.sh — Run Script

Invokes the environment setup script to configure the classpath, then runs the ClassHierarchyExample main class.

Windows:

runJDXExample.bat

Mac/Linux:

chmod +x runJDXExample.sh   # first time only
./runJDXExample.sh

forward.bat / forward.sh — Schema Generation

Creates (or recreates) the database schema based on the ORM specification in the .jdx file, without running the application.

Windows:

forward -create

Mac/Linux:

chmod +x forward.sh   # first time only
./forward.sh -create

JDXDemo.bat / JDXDemo.sh — JDXDemo GUI

Launches the JDXDemo desktop GUI application, which provides a graphical way to browse and interact with the database using the JDX ORM configuration.

Windows:

JDXDemo.bat

Mac/Linux:

chmod +x JDXDemo.sh   # first time only
./JDXDemo.sh

Getting Started

  1. Set JX_HOME to the root of your JDX ORM SDK installation.

  2. Configure the database by editing config/classhierarchy_example.jdx:

    • Update JDX_DATABASE with the correct connection URL and credentials.
    • Update JDBC_DRIVER with the appropriate JDBC driver class.
    • Update setEnvironment.bat (Windows) or setEnvironment.sh (Mac/Linux) to include the JDBC driver JAR on the classpath.
  3. Compile the source files:

    compile.cmd          # Windows
    ./compile.sh         # Mac/Linux
  4. Run the sample application:

    runJDXExample.bat    # Windows
    ./runJDXExample.sh   # Mac/Linux

    The application will automatically create the database schema on first run (controlled by the forceCreateSchema flag in ClassHierarchyExample.java).

Mac/Linux tip: Run chmod +x *.sh once in the project directory to make all shell scripts executable.


Importing into Eclipse

This project can be imported directly into the Eclipse IDE as an existing Java project using File → Import → Existing Projects into Workspace.


Additional Resources

About

Shows how JDX ORM handles Java class inheritance Person → Intern / BaseEmployee → PermEmployee / TempEmployee. Demonstrates polymorphic queries and deep deletes that span an entire hierarchy, and shared sequence-based ID generation across all subclasses.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors