JBoss.orgCommunity Documentation

Chapter 5. Ant Tools

5.1. Introduction
5.2. The <hibernatetool> Ant Task
5.2.1. Basic examples
5.3. Hibernate Configurations
5.3.1. Standard Hibernate Configuration (<configuration>)
5.3.2. Annotation based Configuration (<annotationconfiguration>)
5.3.3. JPA based configuration (<jpaconfiguration>)
5.3.4. JDBC Configuration for reverse engineering (<jdbcconfiguration>)
5.4. Exporters
5.4.1. Database schema exporter (<hbm2ddl>)
5.4.2. POJO java code exporter (<hbm2java>)
5.4.3. Hibernate Mapping files exporter (<hbm2hbmxml>)
5.4.4. Hibernate Configuration file exporter (<hbm2cfgxml>)
5.4.5. Documentation exporter (<hbm2doc>)
5.4.6. Query exporter (<query>)
5.4.7. Generic Hibernate metamodel exporter (<hbmtemplate>)
5.5. Using properties to configure Exporters
5.5.1. <property> and <propertyset>
5.5.2. Getting access to user specific classes

Maybe somebody will find it more preferable to use Ant for generation purposes. Thus, this chapter is intended to get you ready to start using Hibernate Tools via Ant tasks.

The hibernate-tools.jar contains the core for the Hibernate Tools. It is used as the basis for both the Ant tasks described in this document and the eclipse plugins both available from tools.hibernate.org. The hibernate-tools.jar is located in your eclipse plugins directory at /plugins/org.hibernate.eclipse.x.x.x/lib/tools/hibernate-tools.jar.

This jar is 100% independent from the eclipse platform and can thus be used independently of eclipse.

To use the ant tasks you need to have the hibernatetool task defined. That is done in your build.xml by inserting the following xml (assuming the jars are in the lib directory):


<path id="toolslib">
 <path location="lib/hibernate-tools.jar" />
 <path location="lib/hibernate3.jar" />
 <path location="lib/freemarker.jar" />
 <path location="${jdbc.driver.jar}" />
</path>
   
<taskdef name="hibernatetool" 
         classname="org.hibernate.tool.ant.HibernateToolTask" 
         classpathref="toolslib" />

This <taskdef> defines an Ant task called hibernatetool which now can be used anywhere in your ant build.xml files. It is important to include all the Hibernate Tools dependencies as well as the jdbc driver.

Notice that to use the annotation based Configuration you must get a release.

When using the hibernatetool task you have to specify one or more of the following:


<hibernatetool
  destdir="defaultDestinationDirectory"
  templatepath="defaultTemplatePath"
>
  <classpath ...>
  <property key="propertyName" value="value"/>
  <propertyset ...>
  (<configuration ...>|<annotationconfiguration ...>|
   <jpaconfiguration ...>|<jdbcconfiguration ...>)
  (<hbm2java>,<hbm2cfgxml>,<hbmtemplate>,...)  
</hibernatetool>

Hibernatetool supports four different Hibernate configurations: A standard Hibernate configuration (<configuration>), Annotation based configuration (<annotationconfiguration>), JPA persistence based configuration (<jpaconfiguration>) and a JDBC based configuration (<jdbcconfiguration>) for use when reverse engineering.

Each have in common that they are able to build up a Hibernate Configuration object from which a set of exporters can be run to generate various output.

The following sections describe what the various configurations can do, plus lists the individual settings they have.

A <configuration> is used to define a standard Hibernate configuration. A standard Hibernate configuration reads the mappings from a cfg.xml and/or a fileset.


<configuration
  configurationfile="hibernate.cfg.xml"
  propertyfile="hibernate.properties"
  entityresolver="EntityResolver classname"
  namingstrategy="NamingStrategy classname"
>
  <fileset...>
  
  </configuration>

A <jpaconfiguration> is used when you want to read the metamodel from JPA/Hibernate Annotation where you want to use the auto-scan configuration as defined in the JPA spec (part of EJB3). In other words, when you do not have a hibernate.cfg.xml, but instead have a setup where you use a persistence.xml packaged in a JPA compliant manner.

The <jpaconfiguration> will simply just try and auto-configure it self based on the available classpath, e.g. look for META-INF/persistence.xml.

The persistenceunit attribute can be used to select a specific persistence unit. If no persistenceunit is specified it will automatically search for one and if a unique one is found, use it, but if multiple persistence units are available it will error.

To use a <jpaconfiguration> you will need to specify some additional jars from Hibernate EntityManager in the <taskdef> of the hibernatetool. The following shows a full setup:


<path id="ejb3toolslib">
 <path refid="jpatoolslib"/> <!-- ref to previously defined toolslib -->
 <path location="lib/hibernate-annotations.jar" />
 <path location="lib/ejb3-persistence.jar" />
 <path location="lib/hibernate-entitymanager.jar" />
 <path location="lib/jboss-archive-browsing.jar" />
 <path location="lib/javaassist.jar" /> 
</path>
   
<taskdef name="hibernatetool" 
         classname="org.hibernate.tool.ant.HibernateToolTask" 
         classpathref="jpatoolslib" />

<hibernatetool destdir="${build.dir}">
 <jpaconfiguration persistenceunit="caveatemptor"/>
 <classpath>
  <!-- it is in this classpath you put your classes dir,
   and/or jpa persistence compliant jar -->
  <path location="${build.dir}/jpa/classes" />
 </classpath>

 <!-- list exporters here -->

</hibernatetool>

A <jdbcconfiguration> is used to perform reverse engineering of the database from a JDBC connection.

This configuration works by reading the connection properties either from hibernate.cfg.xml or hibernate.properties with a fileset.

The <jdbcconfiguration> has the same attributes as a <configuration> plus the following additional attributes:


<jdbcconfiguration
  ...
  packagename="package.name"
  revengfile="hibernate.reveng.xml"
  reversestrategy="ReverseEngineeringStrategy classname"
  detectmanytomany="true|false"
  detectoptmisticlock="true|false"
>
  ...
  </jdbcconfiguration>

Exporters are the parts that do the actual job of converting the hibernate metamodel into various artifacts, mainly code. The following section describes the current supported set of exporters in the Hibernate Tool distribution. It is also possible for userdefined exporters, that is done through the <hbmtemplate> exporter.

<hbm2ddl> lets you run schemaexport and schemaupdate which generates the appropriate SQL DDL and allow you to store the result in a file or export it directly to the database. Remember that if a custom naming strategy is needed it is placed on the configuration element.


<hbm2ddl
 export="true|false"
 update="true|false"
 drop="true|false"
 create="true|false"
 outputfilename="filename.ddl"
 delimiter=";" 
 format="true|false"
 haltonerror="true|false"
 >

Exporters can be controlled by user properties. The user properties are specified via <property> or <propertyset> and each exporter will have access to them directly in the templates and via Exporter.setProperties().