Introduction

In this blog we’ll be looking at how to use hibernate tools with just ant. This may be useful if you need to plug some hibernate tasks into your build system or have them scripted in any way. We will create a stand-alone ant build file with the minimum number of required libraries.

Libraries

I’ve extracted hibernate tools jar from eclipse hibernate plugin. You can find it in your eclipse installation directory, under plugins:
plugins/org.hibernate.eclipse_X.X.X.X/lib/tools. Take hibernate-tools.jar and freemarker.jar. From plugins/org.hibernate.eclipse_X.X.X.X/lib/hibernate grab more jars:

  • dom4j-1.6.1.jar
  • log4j-1.2.15.jar
  • slf4j-api-1.5.8.jar
  • slf4j-log4j12-1.5.8.jar

You will also need org.apache.commons.logging.jar and JDBC driver. I use MySQL, so I’ve added mysql-connector-java-5.1.13.jar as well. Put all the jars into “lib” directory. Create other directories: classes, src and generated. Or don’t bother – download the whole lot as I’ve set it up.

Java entities

We will use two sample entity classes (POJOs) for that example: Password and Resource. A Resource can contain many Passwords.

public class Password implements Serializable {
        private static final long serialVersionUID = 1L;
        private int id;
        private String password;
 
        // many-to-one association to Resource
        private Resource resource;
 
        public Password() {
        }
        public int getId() {
                return this.id;
        }
        public void setId(int id) {
                this.id = id;
        }
        public String getPassword() {
                return this.password;
        }
        public void setPassword(String password) {
                this.password = password;
        }
        public Resource getResource() {
                return this.resource;
        }
        public void setResource(Resource resource) {
                this.resource = resource;
        }
}
public class Resource implements Serializable {
        private static final long serialVersionUID = 15146339L;
        private int id;
        private String name;
 
        public Resource() {
        }
        public int getId() {
                return this.id;
        }
        public void setId(int id) {
                this.id = id;
        }
        public String getName() {
                return this.name;
        }
        public void setName(String name) {
                this.name = name;
        }
}

Hibernate configuration

Next, let’s create simple hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
<session-factory>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatetutorial</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">****</property>
  <property name="hibernate.connection.pool_size">10</property>
  <property name="show_sql">true</property>
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.hbm2ddl.auto">update</property>
  <!-- Mapping files -->
  <mapping resource="password.hbm.xml"/>
  <mapping resource="resource.hbm.xml"/>
</session-factory>
</hibernate-configuration>

…and mapping files for password and resource.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 
<hibernate-mapping>
        <class name="net.opensesam.entity.Password" table="password">
                <id name="id" type="long" column="ID" />
                <many-to-one name="resource_id" class="net.opensesam.entity.Resource" />
                <property name="password">
                        <column name="password" />
                </property>
        </class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 
<hibernate-mapping>
  <class name="net.opensesam.entity.Resource" table="resource">
   <id name="id" type="long" column="ID" />
   <property name="name">
     <column name="name" />
   </property>
 </class>
</hibernate-mapping>

Ant build file

Finally we can build build.xml! We’ll first add paths to the required jars, then define new task: hibernatetool. We can then use our new task via <hibernatetool> tag – see the code below, it’s pretty self-explanatory.

<project name="MyProject" basedir="." default="default">
<path id="toolslib">
<path location="lib/dom4j-1.6.1.jar" />
<path location="lib/freemarker.jar" />
<path location="lib/hibernate3.jar" />
<path location="lib/hibernate-tools.jar" />
<path location="lib/log4j-1.2.15.jar" />
<path location="lib/org.apache.commons.logging.jar" />
<path location="lib/slf4j-api-1.5.8.jar" />
<path location="lib/slf4j-log4j12-1.5.8.jar" />
<path location="lib/mysql-connector-java-5.1.13.jar" />
</path>
 
<taskdef name="hibernatetool" 
         classname="org.hibernate.tool.ant.HibernateToolTask" 
         classpathref="toolslib" />
 
<target name="default">
<hibernatetool destdir="./generated">
 <classpath>
  <path location="." />
  <path location="./classes" />
 </classpath>
 
 <configuration configurationfile="hibernate.cfg.xml"/>
 <hbm2ddl export="false" outputfilename="sql.ddl"/>
</hibernatetool>
</target>
</project>

We are running above hbm2ddl task to export database schema into ./generated/sql.ddl file. Other tasks you can use are:

  • hbm2java – generate POJOs
  • hbm2dao – generate DAOs
  • hbmtemplate – generate any custom code you want using templates (aka scaffolding)

Refer to the documentation for more details.

Hibernate annotations

If you prefer to use hibernate annotations, you need to do few changes. Add two extra jars:

  • hibernate-annotations-3.5.6-Final.jar
  • hibernate-jpa-2.0-api-1.0.0.Final.jar

Change <configuration> tag into <annotationconfiguration>:

 <annotationconfiguration configurationfile="hibernate.cfg.xml"/>

Your hibernate.cfg.xml will obviously need to contain pointers to annotated classes, e.g.:

  <mapping class="net.opensesam.entity.Password" />

I have it configured with the open source project “Open Sesame” – you can see the whole configuration in git repository.

You can also download a full source code for the non-annotated example.