Inferno DAO is a command-line tool that reverse engineers database schemas and then generates Java source code for popular persistence frameworks, such as Spring Data JPA and Hibernate ORM.
Inferno DAO is available as a zip file and a tarball from the download page. Once extracted, the Inferno DAO command-line interface can be launched by running the included shell script.
Java Runtime Environment 8 or later must be available on the path. OpenJDK 8 is recommended.
Simply double click the zip file to extract the files. It is recommended that the inferno
folder is moved to C:\Program Files
. Add the bin/inferno.bat
batch file to your path by modifying the PATH
environment variable.
Use the tar
utility to extract the contents of the tarball and then move the inferno
folder to an appropriate directory and add the bin/infero.sh
bash script to the path.
tar xf inferno.tar
Before a database schema can be imported, we need to define a JDBC data source to connect to.
The examples
directory contains some example data source definitions that can be used as a starting point. Here is an example data source definition for a local MySQL instance.
url: "jdbc:mysql://localhost:3306"
connection-properties:
- key: "user"
value: "root"
- key: "password"
value: "secret"
Using the data source defined in the previous section, we can now run the Inferno command-line interface and import a schema.
Some JDBC data sources use the term catalog
and others use the term schema
to represent a database schema. For example, MySQL uses catalog
and Postgres uses schema
to represent schemas.
To determine whether your database uses the term catalog
or schema
, use the list-catalogs
and list-schemas
commands.
Run the following command to show a list of available catalogs in the data source.
./bin/inferno list-catalogs -d mysql-datasource.yaml
This should show output similar to this example:
Data source contains 5 catalogs:
- inferno_openemr
- information_schema
- mysql
- performance_schema
- sys
Run the following command to show a list of available schemas in the data source.
./bin/inferno list-schemas -d postgres-datasource.yaml
This should show output similar to this example:
Data source contains 4 schemas:
- inferno
- information_schema
- pg_catalog
- public
Use the import
command to import a schema. Be sure to use the correct values for catalog
(-c
) and schema
(-s
) as determined in the previous step.
Importing a MySQL catalog:
./bin/inferno import -d mysql-datasource.yaml -c openemr -o openemr.yaml
Importing a Postgres schema:
./bin/inferno import -d postgres-datasource.yaml -s openemr -o openemr.yaml
This should show output similar to this example:
Importing schema 'openemr' from mysql-datasource.yaml ...
Imported 200 tables from schema 'openemr'
The generated schema file simply contains meta-data about each table in the schema. Here is an example.
name: "openemr"
tables:
- name: "addresses"
columns:
- name: "id"
jdbcType: "INTEGER"
columnSize: 10
decimalDigits: 0
nullable: false
autoIncrement: false
- name: "line1"
jdbcType: "VARCHAR"
columnSize: 255
decimalDigits: 0
nullable: true
autoIncrement: false
Now that we have a schema we can generate code using one of the provided code generators. The general syntax for generating code is as follows:
./bin/inferno generate -g GENERATOR \
-s openemr.yaml \
-p com.mycompany.dao \
-d src/generated/java
The currently supported values for GENERATOR are java-dto
, spring-data-jpa
, and hibernate
.
./bin/inferno generate -g java-dto \
-s openemr.yaml \
-p com.mycompany.dao \
-d src/generated/java
./bin/inferno generate -g spring-data-jpa \
-s openemr.yaml \
-p com.mycompany.dao \
-d src/generated/java
The Hibernate code generator has two versions. Use hibernate-classic
to generate a Hibernate Mapping XML file for each table, or hibernate
to generate Java code using annotations (recommended).
./bin/inferno generate -g hibernate \
-s openemr.yaml \
-p com.mycompany.dao \
-d src/generated/java