How to Generate Java Code from an ERwin Model

ERwin generates DDL using template files, one for each of the Database Management Systems it supports.  The template language used is known as TLX and consists of a few basic constructs which are built into macros, which are themselves used to build templates, which are stored in a Forward Engineering Template (FET) file.  Each FET file is specific to a DBMS and is exposed to the user through an IDE accessible from the Forward Engineering Dialog.

Select the Physical Model, then Actions > Forward Engineer > Schema Choose Browse... to select a custom template, or Edit... to edit or create new templates.

Select the Physical Model, then Actions > Forward Engineer > Schema
Choose Browse… to select a custom template, or Edit… to edit or create new templates.

Users can edit these templates to modify the DDL generated.

The TLX template editor

The TLX template editor

However, DDL is just plain text, so why not use these templates to generate other text, such as XML, HTML or application code?

In this instance,  I created a template to take table and column metadata from an ERwin model and translate it into Java classes to represent the entities, their attributes, and the relationships between them.

The idea is simple but also hugely scalable. You could translate as much or as little of the ERwin model metadata into the Java model as required for your application – from just the names of the columns to UDPs – and even use auto-generated methods to store or retrieve data as if it was a database. Although this all runs within the JVM the current state of the model can be easily stored or retrieved, for example by using features from Java’s extensive XML handling libraries.

An example of Java class generation. The columns on the left correspond to the fields on the right.

An example of Java class generation. The columns on the left correspond to the fields on the right.

Such templates can be readily created, deleted and edited from within the Forward Generation feature of ERwin itself. Everything is customisable. You might want to take an existing Java-generation template and change the way in which data type errors are reported, or you might want to store the entities, attributes and other metadata in some entirely different format, such as a series of hash tables.

Java is well known for its ability to interface with a vast array of different applications thanks to an ever-growing catalogue of APIs (both official and unofficial). As a result, TLX templates make ERwin model metadata accessible to virtually any application.

Equally there is no reason why code could not be generated in your language of choice in a similar manner. TLX can also be used as a simple logging or metadata reporting tool.

Contact us to find out more about how TLX templates in ERwin could be used to generate a model fitting your specification.

Read more about the TLX language and editing templates here. If you lack the in-house resources for this kind of work, Sandhill also offer consulting services.

Leave a Reply

Your email address will not be published. Required fields are marked *