This is chapter 2 of rewriting the examples of O’Reillys book „Hibernate – A Developers Notebook“ for using Hibernate 3.0 instead of Hibernate 2.x . To get the context, read chapter 1.

The goal of chapter 2 is to write a hbm file for a single table, generating the corresponding java file and compile it.

A main difference when using Hibernate 3.0 is the ant task definition for hbm2java. It now looks like this:

  1. <taskdef name="hibernatetool"
  2.              classname="org.hibernate.tool.ant.HibernateToolTask"
  3.              classpathref="project.class.path"/>
  4.  
  5.  
  6.     <target name="codegen" depends="prepare"
  7.              description="Generate Java source from the O/R mapping files">
  8.         <hibernatetool destdir="${source.root}">
  9.         <configuration propertyFile="${source.root}/hibernate.properties">
  10.           <fileset dir="${source.root}">
  11.          
  12.             <include name="**/*.hbm.xml"/>
  13.           </fileset>
  14.         </configuration>        
  15.         <hbm2java />
  16.        
  17.      </hibernatetool>
  18.      </target>

Also, the package-name of the schemaexport-task has changed:

  1. <taskdef name="schemaexport"
  2.              classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
  3.              classpathref="project.class.path"/>

Another difference is the jdbc-url for HSQLDB 1.8. You have to insert „file“ as in:

jdbc:hsqldb:file:${data.dir}/music

(in ant task „db“) or

jdbc:hsqldb:file:data/music

in hibernate.properties

I developed offline so I ran into the problem that the XML parser wants to download the mapping-dtd as advertised in the DOCTYPE-section of the hbm.xml file. I stripped off the http://www.hibernate.org part of the url, downloaded the dtd and placed it in the root-directory.

After using Ant 1.6, everything worked fine. (Otherwise I got:

  1. java.lang.NoSuchMethodError: org.apache.tools.ant.Project.createClassLoader
  2.   (Lorg/apache/tools/ant/types/Path;)Lorg/apache/tools/ant/AntClassLoader;
  3.         at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:113)
  4. )...

The generated code reveals some differences:

  • The type of the volume attribute is java.lang.Short, not short „as advertised“. This is because of the xml-attribute „not-null“ set to null. However if you ommit this xml-attribute, java.lang.Short is used again.
  • There is no all-attributes-as-parameters-constructor.
  • There is no equals and no hash-code implementation.

I have not yet any idea about why’s that. Comments are welcome. Point 2 + 3 might be because different code templates.

2 Gedanken zu „„Hibernate – A Developers Notebook“ – migrating to Hibernate 3.0, Chapter 2

  1. Thanks for this. I just started Hibernate and have not been able to figure out what I have been doing wrong, but I will try it with 3.0 and see if I can get it to work.

  2. Thanks for posting about your experiences and tips. They are very helpful.

Kommentare sind geschlossen.