Generating domain classes from hbm files in Hibernate

Hibernate offers the capability to generate both the database schema and the java class files for the domain model when you specify the model using the .hbm.xml files.  You can accomplish this using the hibernate3-maven-plugin. I was not able to find too much clear documentation on how to accomplish this, but I was able to hack together a configuration that works. Once again, you start with Maven installed and make sure that you have the .m2/settings.xml file for Maven pointing to the JBoss nexus repository. See my hibernate tutorial refined for instructions on doing this. Assuming you have Maven installed and the settings for the JBOSS tutorial installed, these are the steps. The lines that you type in commands, don’t type the ‘$’. First, download the sample code using the following command:

$ wget http://brie.com/brian/hibernate/hibcodegen.tgz

Unpack it using tar. This works on GNU/Linux.

$ tar zxvf hibcodegen.tgz

cd into the directory where you unpacked the code.

$ cd hibcodegen

Do an ls of the directory and you should see the pom.xml file. Start two more terminals having the same current working directory as the pom.xml file. In window 1, start the database server, window 2, the database manager, and 3, we will use to complete the rest of the commands. The following is the command to start the database, HSQLDB (all on one line):


$ mvn exec:java -Dexec.mainClass="org.hsqldb.Server" -Dexec.args="-database.0 file:target/data/tutorial -dbname.0 meetings"

Start the dababase manager with the following command (again all on one line!):


$ mvn exec:java -Dexec.mainClass="org.hsqldb.util.DatabaseManagerSwing" -Dexec.args="-url jdbc:hsqldb:hsql://localhost/meetings -driver org.hsqldb.jdbcDriver

You database manager should show the database without any tables. Press Ctrl-R to verify this on the database manager. Now, comes time for the auto code and schema generation. This is true magic!

This will generate the Meeting.java class in the src/main/java/com/brie/domain directory:

$ mvn hibernate3:hbm2java

Examine the pom.xml file for the plugins area. The hbm2java takes the Meeting.hbm.xml and automatically generates the Java class file with getters, setters, etc. Of course, if you modify the Meeting.java class file, and run the command again, it will clobber the file with the newly generated one, but aside from those deficiencies (there are ways around this), this very slick.

To add to the “coolness” factor, the following command generates the shema:
$ mvn hibernate3:hbm2ddl

Go to the database browser, type Ctrl-R, and the new Meeting table will show.

Run a sample program that creates a new object and save it.
$ mvn exec:java -Dexec.mainClass="com.brie.NativeApiIllustration"

Use the database manager to view the object just created.

select * from meeting

Using the .hbm.xml file to generate the java classes and the schema makes for some interesting tool ideas. I hope to envision the day when one can use Violet to generate a class diagram and it generates the hbml.xml files. I hope this provides a concrete example for using hbm2java and hbm2ddl. I am sure there could be improvements to my approach, so I will be looking to find them in the future.

brian

About Brian Lavender

I like to program in C++, Java, and Pascal. I have an admiration for languages like Pascal, Ada, and Eiffel. I work a lot with GNU/Linux systems.
This entry was posted in Hibernate. Bookmark the permalink.