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
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
BaseEmployeeincludesPermEmployeeandTempEmployeeobjects. - Subclass-specific queries with predicates (e.g., all
BaseEmployeeobjects in theEnggdepartment) 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.
- Java JDK 8 or higher installed and on the system PATH.
- JDX ORM SDK installed. Set the environment variable
JX_HOMEto the SDK's top-level installation directory. - A supported JDBC-compatible database (SQLite is pre-configured; a MySQL example is also included in the
.jdxfile).
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
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.
This declarative file defines the class-to-table mappings and the shared sequence generator. Key elements:
JDX_DATABASEandJDBC_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 sharingidas 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_DATABASEandJDBC_DRIVERto match your local database setup before running.
Five POJOs forming the class hierarchy, each with a no-arg default constructor as required by JDX ORM:
Person— root class withid,name, andDOB(Date).Intern— extendsPerson; addsschool(String).BaseEmployee— extendsPerson; addsdept(String).PermEmployee— extendsBaseEmployee; addssalary(int).TempEmployee— extendsBaseEmployee; addshourlyRate(float).
The entry point of the sample application. It initializes JDX ORM and runs useJDXORM(), which demonstrates the following:
- Deep delete all existing
Personobjects (and all subclass objects) from the database, then verify counts across all classes are zero. - Batch insert three
Personobjects (Barry,Larry,Mary); query and print allPersonobjects. - Batch insert three
Internobjects (Nina,Tommy,Elvis); query and print allInternobjects. - Batch insert three
BaseEmployeeobjects; query allPersonobjects — demonstrating that aPersonquery returns subclass instances too. - Batch insert three
PermEmployeeobjects; queryPermEmployeeobjects, thenBaseEmployeeobjects (which also returnsPermEmployeeinstances). - Batch insert two
TempEmployeeobjects; run further polymorphic queries across the hierarchy. - Filtered queries — e.g., all
Personobjects withname > 'Larry', and allBaseEmployeeobjects withdept = 'Engg'. - Deep delete all
BaseEmployeeobjects withdept = 'Engg'— cascades to matchingPermEmployeeandTempEmployeerows — then verify counts. - Deep delete all remaining
Personobjects and verify all subclass tables are also cleared.
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.
Compiles all Java source files listed in sources.txt and outputs .class files into the bin/ directory.
- Requires
JX_HOMEto be set to the JDX ORM SDK installation directory. - Links against
jxclasses.jar(JDX ORM library) andjson-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.cmdMac/Linux:
chmod +x compile.sh # first time only
./compile.shSets 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 viasource ./setEnvironment.sh).
Invokes the environment setup script to configure the classpath, then runs the ClassHierarchyExample main class.
Windows:
runJDXExample.batMac/Linux:
chmod +x runJDXExample.sh # first time only
./runJDXExample.shCreates (or recreates) the database schema based on the ORM specification in the .jdx file, without running the application.
Windows:
forward -createMac/Linux:
chmod +x forward.sh # first time only
./forward.sh -createLaunches the JDXDemo desktop GUI application, which provides a graphical way to browse and interact with the database using the JDX ORM configuration.
Windows:
JDXDemo.batMac/Linux:
chmod +x JDXDemo.sh # first time only
./JDXDemo.sh-
Set
JX_HOMEto the root of your JDX ORM SDK installation. -
Configure the database by editing
config/classhierarchy_example.jdx:- Update
JDX_DATABASEwith the correct connection URL and credentials. - Update
JDBC_DRIVERwith the appropriate JDBC driver class. - Update
setEnvironment.bat(Windows) orsetEnvironment.sh(Mac/Linux) to include the JDBC driver JAR on the classpath.
- Update
-
Compile the source files:
compile.cmd # Windows ./compile.sh # Mac/Linux
-
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
forceCreateSchemaflag inClassHierarchyExample.java).
Mac/Linux tip: Run
chmod +x *.shonce in the project directory to make all shell scripts executable.
This project can be imported directly into the Eclipse IDE as an existing Java project using File → Import → Existing Projects into Workspace.
- JDX Database & JDBC Driver Specification Guide
- JDX ORM SDK documentation (included in your SDK installation under
JX_HOME)