Kategorien
Java

„Hibernate – A Developers Notebook“ – migrating to Hibernate 3.0, Chapter 3

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.