Hibernate Code Generation

Justin left a comment about various tools for generating Java code from XML. Last week while I was reading about Hibernate I was interested to read that the same files you use to persist POJO’s to a database using Hibernate can be used to create the Java objects as well. It’s reasonably well documented on the hibernate site, but doesn’t include all the classes you need on the classpath, so here’s an example from my system:

java -cp C:\hibernate\hibernate-2.1\hibernate2.jar;
C:\hibernate\hibernate-extensions-2.1\tools\hibernate-tools.jar;
C:\hibernate\hibernate-2.1\lib\commons-collections.jar;
C:\hibernate\hibernate-2.1\lib\commons-logging.jar;
C:\hibernate\hibernate-extensions-2.1\tools\lib\jdom.jar net.sf.hibernate.tool.hbm2java.CodeGenerator Resource.hbm.xml

where Resource.hbm.xml looks likes this:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
 PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="com.mycompany" auto-import="false">
 <class name="Resource" table="resource">
  <id name="id" type="int" unsaved-value="0" >
    <column name="id" sql-type="int" not-null="true"/>
    <generator class="identity" />
   </id>
   <property name="label" type="string" />
   <property name="filename" type="string" />
   <property name="teaser" type="string" />
   <property name="type" type="string" />
   <property name="url" type="string" />
   <property name="datetimecreated" type="date" />
   <property name="datetimemodified" type="date" />
   <property name="createdby" type="string" />
   <property name="modifiedby" type="string" />
   <property name="active" type="integer" />
   <property name="archive" type="integer" />
 </class>
</hibernate-mapping>

This will automatically create a JavaBean, with full and empty constructors, getters and setters for all the fields, a toString() method, an equals() method, and a hashCode() method. Additionally, you can use an external configuration file to add an interface that the bean implements, change the rendering mechanism, the package, etc.

Cooler still, you can go backwards and create Hibernate mapping files directly from your existing compiled classes using the net.sf.hibernate.tool.class2hbm.MapGenerator.

6 thoughts on “Hibernate Code Generation”

  1. Hey AJ, FYI… this entry causes your blog to layout really really wide in Mozilla Firebird (probably other browsers as well, at least the Mozilla-derived ones).

  2. I prefer to use Middlegen. I reverse engineer the database and get the code and the mapping files. Minimum work involved.

  3. Can you tell me detail? i have already use tool net.sf.hibernate.tool.class2hbm.MapGenerator but it don’t work. I don’t know why. Can you send me one example?

    Thank alot
    Long

  4. Don’t forget to mention that without a type= attribute on the property, no getter/setter is generated.

Leave a Reply

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