Member Menu
 
 Monthly JBoss newsletter:
 
Java Persistence with Hibernate
CaveatEmptor

Tools for Hibernate 2.x

The tools used for roundtrip development in Hibernate 2 provide support for transforming to and from sourcecode (java), mappingfiles (hbm) and a database definition (ddl). Use the new tools for Hibernate 3.x.

The goal of these tools is to help support the usage of Hibernate in almost any kind of development scenario. For example, if you start with an existing (legacy) database, use the tools to generate Hibernate mapping files and Java source files. If you start with Java code, you can markup your classes with XDoclet tags and automatically generate Hibernate mapping files from your annotations. Of course, you can also generate a SQL database schema (DDL) automatically from a Hibernate mapping.

Note, that not all tools provide complete support for all features in Hibernate, as it is simply not possible to generate every output from every input (for example, a database schema doesn't have enough information to generate perfect Java classes).

See the illustration on the right to get an overview of the roundtrip process and the tools used in each step.

AndroMDA: This tool can use XML files in the XMI format (generated by UML modeling tools such as Poseidon/ArgoUML) and generate Java source files automatically. Use it to generate the POJO classes of your domain model, documentation can be found in the Community Area and on the AndroMDA website.

XDoclet: With XDoclet, you place annotations right in your Java source files (in Javadoc comments). XDoclet will then read this markup and generate Hibernate mapping files automatically. The defaults are reasonable for most cases, this procedure therefore minimizes typing. You can of course also merge/add your own mapping files in the process. Note that XDoclet does (conceptually) not support some more exotic Hibernate mappings (entity associations with composite keys, for example). See the XDoclet documentation in the Community Area for an example and the XDoclet website for downloads and more information.

SchemaExport (hbm2ddl): This tool is provided with the core Hibernate download. It uses Hibernate mapping metadata XML files to generate a SQL database schema with DDL. You can enhance your mapping files with database specific elements (SQL column datatypes, unique/check constraints/indexes, etc.) and then export the SQL DDL to a text file. You may also directly export the DDL to a database, this is very powerful in development, as you can automatically create and drop a database at each test run. See the Hibernate reference documentation for more information about this tool.

Middlegen: If you have a legacy database, Middlegen is the right tool for you. With the Hibernate plugin, Middlegen can generate Hibernate mapping files. You may also customize the mapping files with a GUI. See the Middlegen website for more information.

CodeGenerator (hbm2java): This tool is part of the separate Hibernate Extensions download. It reads Hibernate mapping XML files and generates Java code for your domain/business and persistence layer (finder method generation). It is extremely flexible and can even be customized with Velocity templates. A nice trick is to automatically generate XDoclet Javadoc tags, so you will have a full roundtrip cycle starting from the top again. See the Hibernate reference documentation for more information.

You may also find references to some older tools, such as class2hbm and ddl2hbm. They have been replaced by the more powerful XDoclet and Middlegen and are considered deprecated. If you still like to use them, see the Hibernate reference documentation and the Hibernate Extensions package.

Quickstart Workshop

This workshop was held at the JAOO conference in Denmark in October 2003. The presentation shows the Hibernate toolset for roundtrip development, including database DDL/schema generation and Java class generation from existing database schemas. The presentation also contains a lab you can walk through, along with the source and build files required. An example database using HSQLDB is included.

Show presentation [HTML], Download presentation [OpenOffice], Browse examples, Download examples

Hibern8IDE

The predecessor of our new Hibernate3 graphical toolset, Hibern8IDE, offers a Swing-based interface for HQL and Criteria query execution, and result set browsing. It is considered legacy, active development will only take place for the Hibernate3 toolset. Hibern8IDE is also distributed as part of the separate Hibernate Extensions package.

Hibernate in Action

Hibernate 2.x and the toolset are also covered in Hibernate in Action, First Edition.

© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]