Chapter 8 introduces Criteria Queries. Only QueryTest.java 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));

to

  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 StereoVolumeType.java and SourceMediaType.java . 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 SourceMediaType.java:

  1. public Object replace(Object original, Object target, Object owner)
  2.     throws HibernateException {
  3.  
  4.         return original;
  5.    }
  6.  
  7. public Serializable disassemble(Object value) {
  8.        return (Serializable) deepCopy(value);
  9.  }
  10.  
  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.     }
  17.  
  18. public int hashCode(Object o) { return o.hashCode(); }

For StereoVolumeType.java:

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

That’s it.

As in chapter 4, copy the hbm.xml-files and change the DTD-reference. Leave CreateTest.java, QueryTest.java and QueryTest2.java alone – they will still compile. Copy the AlbumTest.java 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 CreateTest.java again according to Java-beans coding style.
The package name in QueryTest.java and QueryTest2.java 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 CreateTest.java:

  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));
  7.  session.save(track);
  8.  
  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));
  15.  session.save(track);
  16.  
  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));
  23.  session.save(track);

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>
  8.  
  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"/>
  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.

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 1.8.0.2 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 hibernate-tools-3.1.0.alpha5.zip 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:

ant-1.6.2.jar
ant-antlr-1.6.2.jar
ant-junit-1.6.2.jar
ant-launcher-1.6.2.jar
antlr-2.7.5H3.jar
asm-attrs.jar
asm.jar
cglib-2.1.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.jar
ehcache-1.1.jar
hibernate-tools.jar
hibernate3.jar
hsqldb.jar
jdbc2_0-stdext.jar
jta.jar
jtidy-r8-21122004.jar
log4j-1.2.9.jar
velocity-1.4.jar
velocity-tools-generic-1.1.jar
xerces-2.6.2.jar
xml-apis.jar

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