28 October 2005

Issues Upgrading from Middlegen hibernate 2 tools to hibernate 3 tools

Reason for move

The hibernate tools shipped with Middlegen 2.1 generates equals/hashcode based on the ID of the object. This is a well-known issue that makes it impossible to add multiple new objects in a Set. I haven't had this problem up to now.

I tried adding all of the following to the ant task:
  • equalsHashcode="true"
  • columnsIncEquals="true"
  • columnsIncToString="true"
It seems to generate toString() correctly, but equals() and hashcode() still do not contain all fields. The hbm files do seem to contain the correct meta-data though.

<update>
It appears the version of hibernate-tools.jar that I'm using was old. I downloaded hibernate-extensions-2.1.3 and used the hibernate-tools.jar from that. It seemed to solve the equals-hashcode issue. The remainder of this post servers as a reminder of what I need to do to upgrade to hibernate 3 tools...
</update>

I figured I'd try to move to hibernate 3 tools.

Finding Hibernate 3 tools

I went to the Hibernate Tools page and clicked Download. The version listed there is for Hibernate 3.1. (I tried it with hibernate 3, but it doesn't work; a particular class is missing a method). I'm not prepared to upgrade to Hibernate 3.1 yet as it is not officially released.

I tried the All Downloads for Hibernate page. It seems the latest Hibernate 3.0.x based tools is hibernate-tools-3.0.0.alpha4a.zip. It looks like the they're only going to release hibernate tools on 3.1 so the alpha4a is the last one for hibernate 3.0.

I had to switch out the task with a task as documented on the Hibernate Ant Tools documentation.

Problems Encountered
  1. Classes no longer have full constructors. This mainly caused problems in unit tests; I often populate composite keys using the constructor. All this code needs fixed.
  2. boolean types in the hbm file seem to get mapped to Boolean objects instead of primitives. This probably makes sense, but it is code that needs fixed and tested. The getter signatures have changed from boolean isSomething() to Boolean getSomething().
OK. So the constructor issue is mentioned on the Hibernate Forums. It looks like there are bugs in toString and equals/hashcode that have only recently been committed to CVS and probably only available in the Hibernate 3.1-based tools. Hmm... I think I'll forget about the Hibernate 3.0.x-based tools for now.