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:
- <taskdef name="hibernatetool"
- classname="org.hibernate.tool.ant.HibernateToolTask"
- classpathref="project.class.path"/>
- <target name="codegen" depends="prepare"
- description="Generate Java source from the O/R mapping files">
- <hibernatetool destdir="${source.root}">
- <configuration propertyFile="${source.root}/hibernate.properties">
- <fileset dir="${source.root}">
- <include name="**/*.hbm.xml"/>
- </fileset>
- </configuration>
- <hbm2java />
- </hibernatetool>
- </target>
Also, the package-name of the schemaexport-task has changed:
- <taskdef name="schemaexport"
- classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
- 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:
- java.lang.NoSuchMethodError: org.apache.tools.ant.Project.createClassLoader
- (Lorg/apache/tools/ant/types/Path;)Lorg/apache/tools/ant/AntClassLoader;
- at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:113)
- )...
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 Antworten auf „„Hibernate – A Developers Notebook“ – migrating to Hibernate 3.0, Chapter 2“
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.
Thanks for posting about your experiences and tips. They are very helpful.