Chapter 8 introduces Criteria Queries. Only is affected. Besides the usual net.sf.hibernate to org.hibernate package import renaming, net.sf.hibernate.expressions in Hibernate 2 is replaced by org.hibernate.criterion.

Moreover, change the line

  1. Example example = Example.create(new Artist(namePattern, null, null));


  1. Artist artist = new Artist();
  2. artist.setName(namePattern);
  3. Example example = Example.create(artist);

because Hibernate 3 has generated no argument constructors only.

Chapter 7 is working in Hibernate 3 (as opposed to chapter 6). The most challanging in this chapter migrationwise are and . Change the import-package names. The Usertype-stuff is now under the package „org.hibernate.usertype“. It won’t compile, yet as there are some methods missing. For

  1. public Object replace(Object original, Object target, Object owner)
  2.     throws HibernateException {
  4.         return original;
  5.    }
  7. public Serializable disassemble(Object value) {
  8.        return (Serializable) deepCopy(value);
  9.  }
  11. public Object assemble(Serializable cached,
  12.                            Object owner)
  13.     {
  14.         // Our value type happens to be serializable, so we have an easy out.
  15.         return deepCopy(cached);
  16.     }
  18. public int hashCode(Object o) { return o.hashCode(); }


  1. public Object replace(Object original, Object target,SessionImplementor session,
  2.                       Object owner)
  3.     throws HibernateException {
  5.                    return deepCopy((StereoVolumeType)original);
  6.   }
  8. public int hashCode(Object o) { return o.hashCode(); }
  10. public Object replace(Object original, Object target, Object owner)
  11.                throws HibernateException {
  13.                    return original;
  14.  }

That’s it.

You can skip the entire chapter 6 if you use Hibernate 3. It is based on the interface PersistenceEnum which already became deprecated in Hibernate 2 as the author points out in the errata. The interface has apparently removed in Hibernate 3.

As in chapter 4, copy the hbm.xml-files and change the DTD-reference. Leave, and alone – they will still compile. Copy the file, change the hibernate-imports and the constructor calls as well as int parameters to Integer.

This should be easy as we have done that before.

In this chapter, we add an Entity called „Artist“ as well as an entity called „Comments“.

Copy both Track.hbm.xml and Artist.hbm.xml from the examples-distribution and remember to change the DTD-reference if necessary.
When using „ant schema„, SAX complained it couldn’t find ${src.root}/com/oreilly/hh/hibernate-mapping-2.0.dtd for Artist.hbm.xml.
That’s strange because it didn’t complain about it in Track.hbm.xml before. However, I place the DTD file in ${src.root}/com/oreilly/hh
and the prepare-target now copies *.dtd file, too.

The code generation is no problem. I has changed again according to Java-beans coding style.
The package name in and needs to be changed again from „net.sf.hibernate...“ to „org.hibernate...“ and everything works.

So that’s it for chapter 4.

In chapter 3 (you can read it online) we use the Track table to insert some data and to query data.

We use CreateTest to create and QueryTest to query data. Some simple adaptions are necessary:

  • change package name of hibernate classes to org.hibernate
  • replace the use of the all-attributes-as-parameters-constructor with Java-Bean style use of empty constructors and setters
  • change the type of „volume“ attribute from short to java.lang.Short

Here is the changed part of

  1. Track track = new Track();
  2.  track.setTitle("Russian Trance");
  3.  track.setFilePath("vol2/album610/track02.mp3");
  4.  track.setPlayTime(Time.valueOf("00:03:30"));
  5.  track.setAdded(new Date());
  6.  track.setVolume(new Short((short)0));
  9.  track = new Track();
  10.  track.setTitle("Video Killed the Radio Star");
  11.  track.setFilePath("vol2/album611/track12.mp3");
  12.  track.setPlayTime(Time.valueOf("00:03:49"));
  13.  track.setAdded(new Date());
  14.  track.setVolume(new Short((short)0));
  17.  track = new Track();
  18.  track.setTitle("Gravity's Angel");
  19.  track.setFilePath("vol2/album175/track03.mp3");
  20.  track.setPlayTime(Time.valueOf("00:06:06"));
  21.  track.setAdded(new Date());
  22.  track.setVolume(new Short((short)0));

QueryTest needs only substitution of hibernate package names.

I also created a ant-target called „clean-db“ which deletes the entire schema in order to avoid multiple insertion of the same data as well as an ant-target called „clean“ which simply deletes the „classes“-folder.

  1. <target name="clean-db" description="deletes schema of DB.">
  2.     <delete>
  3.       <fileset dir="${data.dir}">
  4.         <include name="**/*"/>
  5.       </fileset>
  6.     </delete>    
  7.   </target>
  9.   <target name="clean" description="clean ${class.root}">
  10.     <delete dir="${class.root}"/>
  11.   </target>

So now you can say

  ant clean-db clean schema codegen ctest qtest

and you get a clean db with a new schema, freshly generated code, test data generation and query test – in one command.

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"/>
  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}/">
  10.           <fileset dir="${source.root}">
  12.             <include name="**/*.hbm.xml"/>
  13.           </fileset>
  14.         </configuration>        
  15.         <hbm2java />
  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:


(in ant task „db“) or



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 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:
  2.   (Lorg/apache/tools/ant/types/Path;)Lorg/apache/tools/ant/AntClassLoader;
  3.         at org.hibernate.tool.ant.HibernateToolTask.execute(
  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.

In this blog series I want to go through the examples in James Elliotts book „Hibernate – a developers notebook“ using
object/relation mapping service Hibernate 3.0 instead of Hibernate 2.x that this book was written about and noting the differences, i.e. what I had to change to get them working.

I enjoyed reading „Hibernate – a developers notebook“ by James Elliott . In order to put my hands on hibernate I want to try out the examples of the book. While it describes Hibernate 2 I want to try it out with Hibernate 3.0 . Not having used Hibernate before I’m a complete newbie and may stumble over things an experienced Hibernate-developer has no problems with.

In the first chapter we are encouraged to download Apache Ant, Hibernate and HSQLDB (a free pure-Java DBMS that is used throughout the book).

I didn’t download Ant in the first place because I already had Ant 1.5 installed. Foreshadowing part 2, it will turn out that Apache Ant 1.6 is required for the HibernateToolTask so I later installed that.

I then downloaded HSQLDB without problems.

From some Java Magazin CD-ROM I used a source distribution of Hibernat 3.0.1, installed and compiled it. In order to create
java-files from hbm-files we need hibernate-tools.jar which we get by downloading and extracting it from org.hibernate.eclipse_3.1.0.alpha5.jar that lives within that zip-file.

Finally, I ended up with the following files in the examples lib directory:


I’m not sure if every single jar is required (especially the ant-xxx.jar files).