Object Relational Mapping

The javaxt-orm library is a handy tool for generating Java code and DDL from a set of models defined in a Javascript or JSON document. The generated classes are used to persist data in a relational database via the javaxt.sql.Model class.

Download javaxt-orm
Current Version: 1.0.0
Release Date: 1/15/2023
File Size: 35 KB
File Format: Zip
Includes: Jar File, Binaries, Source Code, and Documentation

Key Features

  • Auto-generate database schema and Java classes
  • Command line interface
  • Supports spatial geometry types with JTS
  • Plug and play with javaxt-core and javaxt-express
  • Supports common RDMBS (PostgreSQL, H2, etc)

Note that this is not intended to be a full fledged ORM framework. Instead, the goal is to help jumpstart new projects by providing a simple utility for stubbing out code and SQL.

Basic Usage

The javaxt-orm library provides a command line interface that can be used to generate Java classes and schema. All you need to do is provide a input model and an output directory. Example:

java -jar javaxt-orm.jar /path/to/model.js /output

Model Input

Below is a simple example of an input Javascript file with an Address model.
var package = "com.example.models";
var models = {
    Address: {
        fields: [
            {name: 'street',        type: 'string'},
            {name: 'city',          type: 'string'},
            {name: 'state',         type: 'string'},
            {name: 'postalCode',    type: 'string'},
            {name: 'coordinates',   type: 'geo'}
        ]
    }
}

The full list of field types are listed below.

In addition to the standard field types, you can specify a model as a `type`. In the example below, the address field in the Contact model is a `Address` type.

{
  Contact: {
     fields: [
        {name: 'name', type: 'string'},
        {name: 'address', type: 'Address'}
     ]
  },
  Address: {
      ...
  }  
}

Model mapping and supported types

Field TypeJava TypeDatabase TypeComments
intIntegerinteger
longLong bigint
floatDoubledouble precision
doubleDoubledouble precision
decimalBigDecimalnumeric
numericBigDecimalnumeric
textStringtext or varcharvarchar if there is a length constraint
stringStringtext or varcharvarchar if there is a length constraint
charStringchar(1)
booleanBooleanBoolean
dateDate timestamp with time zonejavaxt.utils.Date
binarybyte[]bytea
jsonJSONObjectjasonbjavaxt.json.JSONObject
geoGeometrygeometry(Geometry,4326)For lat/lon geographic data. Geometry is found in the JTS package.
geometryGeometrygeometry(GeometryZ)For x,y,z data. Geometry is found in the JTS package.
passwordStringtextStores bcrypt hash in the database (vs plaintext password)

Supported field constraints

Constraint KeyTypeComments
requiredbooleanCan also use "nullable" keyword.
lengthintOnly applies to "text" field types. Reassigns the database type to varchar.
uniquebooleanShould not be applied to "text" fields without a length constraint.

Misc

  • IDs are automatically added so you don't have to explicitly define one in the model.
  • Models can be assigned to fields. A foreign key will be created in the database.
  • lastUpdate fields in the model are automatically assigned a trigger in the database.

Dependencies

The javaxt-orm library requires Java 8 or higher for Javascript parsing and javaxt-core.jar for JSON and basic file IO.

Generated Code Dependencies

The javaxt-orm library generates Java code that extends/implements javaxt.sql.Model class. It also calls java.util.Map.ofEntries which was introduced in Java 9. Therefore, you will need both javaxt-core and Java 9 (or higher) to use the generated code in your project. In addition, you will need JTS if you include a `geo` or `geometry` type. Last but not least, you will need to include a JDBC driver in your project for persistance.