kopf.lastig | Alles mögliche zu Themen wie Familie, Beruf, Medien etc. …

Sep/05

6

“Hibernate – A Developers Notebook” – migrating to Hibernate 3.0, Chapter 2

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:

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

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

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

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:

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

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.

Im Web 2.0 mit anderen teilen:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • Add to favorites
  • DZone
  • email
  • FriendFeed
  • MisterWong.DE
  • MySpace
  • PDF
  • Reddit
  • RSS
  • Tumblr
  • Twitter

· ·

2 comments

  • chl · September 30, 2005 at 23:49

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

  • Kieran · January 22, 2006 at 21:50

    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.

Leave a Reply

<<

>>

Theme Design by devolux.nh2.me