Search

August 30, 2012

Using ANT Hibernate Tools with Hibernate 4

Recently I have been upgrading a JEE application to the latest versions of the libraries used. In particular I was upgrading from Hibernate 3 to Hibernate 4.

In this particular application, we maintained Hibernate mapping files and used Hibernate Tools to generate the schema via an ANT task. At first glance, it seemed that Hibernate Tools had been completely absorbed into JBoss Tools for Eclipse. It also appeared that Hibernate Tools did not support Hibernate 4. However, it turned out I was able to get what I needed.

So the first trick was locating Hibernate Tools. From the download page for JBoss Tools (http://www.jboss.org/tools/download/), drill into the download link at the bottom for the version of JBoss Core Tools you want. In my case the latest was 3.3. On this page you will find separate downloads for JBoss Tools and Hibernate Tools. I downloaded the zip for Hibernate Tools.

From here you will find that the download consists of plugins for Eclipse. But if we tear apart the right one we can get to the hibernate-tools.jar file needed for the ANT task. In this case the right jar was plugins/org.jboss.tools.hibernate4_0_3.5.1.v20120715-0252-H98-Final.jar.

After exploding the jar I found the hibernate-tools.jar in the lib/tools subdirectory. Unfortunately my fun was not over. I soon ran into this problem:

java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

I needed to include the jars from the lib/required directory as well. But we were still not quite there:

[hibernatetool] SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
[hibernatetool] SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

BUILD FAILED
c:\eng\projects\sc\build.xml:83: java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

Luckily the error message at the indicated URL was informative. I downloaded a implementation of slf4j and included the slf4f-nop.jar in the classpath. (Note that using the slf4j-log4j jar from the lib directory caused all the useful output from the hibernatetool ANT task to be suppressed. And I did not want to set up a log4j configuration just for this. Have I mentioned how frustrating all these logging frameworks are?)

As a bonus, here’s one more issue I encountered while upgrading Hibernate Tools:

org.hibernate.MappingException: Could not determine type for: org.jasypt.hibernate4.type.EncryptedStringType

Hibernate Tools was having issues with the properties configured to be encrypted via jasypt. (An excellent way to include encrypted data into a database transparently, by the way.) The trick turned out to be to define the sql-type attribute on the column element under property element in the mapping file.

In case you were wondering, I was able to get Hibernate Tools to generate POJOs, documentation and mapping files. I’m not using anything specific to Hibernate 4 so I don’t know if I can declare Hibernate Tools completely compatible with it, but it seems a great deal of functionality is available, if you work a little.