Your first cup of Hibernate …

Hibernate is pretty much the defacto tool/library/framework to implement Object Relational Mapping (ORM) in Java nowadays and it has definitely become a much larger project than when I first started using it in 2004. It has so many downloads (Core, Annotations, Shards, etc.) and so many jars and dependencies that it is quite difficult to decide what is required and what is not required (and what is important and what is optional).

Here I try to provide some clarity by starting with a basic Hibernate project and slowly working up (using more advanced features of Hibernate) and in the process discovering the different features of Hibernate (and their dependencies).

All the code mentioned here is available via the Google Code Project – DalalStreet.

So let us start from scratch – which means no downloads from hibernate.org, unless we absolutely need it – and proceed step by step :

  • I downloaded Eclipse 3.4.1 and created a Java Project – DalalStreet.
  • I decided to use Java annotations and Eclipse immediately gave me an error (see screenshot below)
  • Annotation Errors

    Annotation Errors

  • This can be resolved by adding ejb3-persistence.jar to the classpath of your Eclipse project. For this you need to download the hibernate annotations (I decided to use version 3.2.1 – pay special attention to the compatibility matrix) and the ejb3-persistence.jar is located in the lib folder.
  • After finishing all the annotations, you will want to export the schema, but before you can do that you need to define a hibernate config file.
  • To export the schema, I decided to use the Ant HibernateToolTask. Of course Ant didn’t know where to find this class. For this, you need to download the hibernate tools (I am using version 3.2.4), unzip it, and navigate to the plugins folder. Now navigate to the lib/tools folder inside the org.hibernate.eclipse_<version_number> folder and you will find hibernate-tools.jar (add this to the classpath of ANT).
  • After you have added the hibernate-tools.jar to the classpath you will require the following jars (to be added to the classpath of ANT)
    • hibernate3.jar from hibernate core (for obvious reasons, download hibernate core and add hibernate3.jar – found in the top level folder)
    • commons-logging-1.0.4.jar (in the above hibernate core download, lib folder)
    • hibernate-annotations.jar (in hibernate annotations download, root folder)
    • dom4j-1.6.1.jar (in hibernate core download, lib folder)
    • commons-collection-2.1.1.jar (in hibernate core download, lib folder)
    • freemarker.jar (in the hibernate tools download, inside the lib/tools folder of the org.hibernate.eclipse_<version_number> folder)
    • and finally the jdbc driver jar which is of course dependent on the database you are using (I am using the MySQL database and the following driver jar : mysql-connector-java-5.1.7-bin.jar)

    Once you have all this in place, your ANT task should complete successfully and you should have a valid schema in your database.

    A final screenshot with all the jars in your lib folder :

    Hibernate and dependent jars

    Hibernate and dependent jars

    Hope you found this post helpful. The next post will discuss the next logical step – actually persisting some data into the database using Hibernate.

1 comment so far

  1. [...] Posted May 20, 2009 Filed under: software | Tags: java, hibernate, one-to-one | In an earlier post I had written about getting your development environment setup to start using Hibernate. I had [...]


Leave a reply