Code Generation and OR Mapping

Code Generation and O/R Mapping

The database is a rich source of metadata, making the writing of data access code a prime candidate for code generation techniques. As we learned in Chapter 6, Code Generation, active code generation can help increase developer productivity and promote project agility.

The next sections cover by example how O/R mapping tools can combine with code generation techniques to automate the generation of all data access code for a project. The example covers the use of code generation and O/R mapping tools from the perspective of the data model driving the process, as this is the typical scenario for enterprise software.

Two open source tools are used in the example: Hibernate and Middlegen. Hibernate offers O/R mapping, while Middlegen is a database-driven code generation tool, able to generate mapping files for a variety of O/R mapping technologies, including entity beans, JDO, and Hibernate.


Using code generation for producing data access code is not exclusive to O/R mapping products. Entity beans and standard JDBC calls can both benefit from code generation techniques.

The example has a number of steps that demonstrate an end-to-end generation process from entity model to database and then back to an object models:


Create a database.


Define the entities of a data model using a modeling tool.


Use the modeling tool to forward-engineer the script to create a database schema.


Create the database schema.


Use Middlegen to construct Hibernate mapping files by reverse engineering from the database schema.


Use the Hibernate tool, hbm2java, to create plain Java classes from the mapping files.


With the modeling tool, reverse-engineer the generated Java code to view the object model.


Compare the original data model against the generated object model.

First, we look at the two main products used in the example.

Introducing Hibernate

Hibernate is an open source O/R mapping product. Unlike entity beans, which are heavyweight components, Hibernate allows standard Java objects, or POJOs, to be transparently written and retrieved to and from the underlying data store.

As with commercial O/R mapping tools, Hibernate supports enterprise-level capabilities, including transactions, connection pooling, a powerful SQL-like query language, and the ability to declaratively define entity relationships. The tool also integrates with J2EE, making it suitable for both J2SE-based and J2EE-based applications.

Hibernate uses configuration files to hold metadata describing mapping settings and entity relationship information. The Hibernate configuration files are one of the tool's key strengths. They offer extensive configuration options, making it possible to precisely define the behavior of the persistence framework. Such configuration precision ensures Hibernate can be tuned to deliver a high-performance persistence solution.

Hibernate supports the definition of inheritance, association, and composition relationships types, in addition to the specification of relationships based on Java collections. Hibernate supports most of the leading RDBMS products.

Although Hibernate is open source, it is nevertheless a proprietary API. Unlike JDO, it has not gone through the JSR process and consequent community review. Despite this, the Hibernate development team has arguably eclipsed JDO by providing a robust implementation with a highly detailed level of documentation. Consequently, Hibernate has enjoyed good word of mouth regarding the quality of the product, and developers have been quick to embrace Hibernate as the open source mapping tool of choice. The Hibernate team claims the product is the leading O/R mapping toolkit for Java.

The Hibernate binaries, source, documentation, and examples can be downloaded from Hibernate is available under the LGPL license, which allows commercial use. As usual, check with the site for full licensing details.

Introducing Middlegen

Over the years, various code generators have emerged that use database metadata to drive the code generation process. Middlegen is one such open source product that has proven popular with developers and complements Hibernate's persistence capabilities by taking on the chore of generating Hibernate mapping files.

Middlegen uses JDBC to access database metadata and the Velocity engine for code generation. The use of JDBC enables Middlegen to support most of the major RDBMS vendors. Like XDoclet, Middlegen uses an Ant build file for launching the product.

The Velocity template engine and XDoclet are discussed in Chapter 6.

Middlegen uses a plug-in approach for code generation. After Middlegen has determined the database structure, the task of generating code is handed over to those plug-in generators that have been configured with the <middlegen> Ant build task. At the time of writing, plug-ins are available for generating entity beans, JDO classes, and Hibernate mapping files.

The example focuses on generating Hibernate mapping files; however, Middlegen is not constrained to this one persistence technology. The ability to produce code for more than one type of persistence mechanism is vital, as either the architecture or customer requirements may mandate the persistence technology used. The plug-in architecture of Middlegen provides the ability to accommodate such changes.

To download the latest version of Middlegen, along with full documentation and a comprehensive sample application, visit


As with all successful software technologies, ongoing development work continues to refine and improve the product. This is certainly the case with Middlegen and Hibernate. At the time of writing, I am using versions 2.x of both Middlegen and Hibernate. Middlegen currently has a version 3.0 in the pipeline, while Hibernate is producing a new version of its Middlegen plug-in for version 3.0. The release strategy is to make all of these betas final at the same time.

So as not to be totally out of date, the example uses a preview of the new Hibernate plug-in, which is available for download from the Hibernate site.

With all the necessary software installed, we can move on to generating the persistence layer. However, before we can look at how code generation techniques can combine with O/R mapping tools to save us time and effort, we need a RDBMS installed and an operational database established with a suitable schema.

The next section covers the setup of the RDBMS and creation of the database schema required for the example.

    Rapid J2EE Development. An Adaptive Foundation for Enterprise Applications
    Rapid J2EEв„ў Development: An Adaptive Foundation for Enterprise Applications
    ISBN: 0131472208
    EAN: 2147483647
    Year: 2005
    Pages: 159
    Authors: Alan Monnox © 2008-2017.
    If you may any questions please contact us: