Hibernate Class Generation Using hbm2java
Pages: 1, 2, 3, 4, 5, 6
Generating the Classes for a Real-World Project
Actually, hbm2java is designed to convert
one hibernate mapping file into a corresponding set of
Java classes. If you want to use this approach for a real
application, it would be obviously more convenient to generate the
classes for all of the Hibernate mapping files in one fell
swoop. The best way to do this is integrate the class-generation
task into your automated build process.
Integrating into an Ant Build Process
Invoking hbm2java using Ant is fairly straightforward. First,
you need to declare the hbm2java task so that Ant can
invoke it:
<taskdef name="hbm2java"
classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
classpathref="project.class.path"/>
Next, you use this task; for example, by writing a target to
generate source code for all of the *.hbm.xml files in the
source directory. Suppose ${src.hibernate} represents the
directory containing the Hibernate mapping files, and
${src.generated} the directory where you want the source
code to go. The Ant task could look something like this:
<target name="codegen"
description="Generate Java source code
from the Hibernate mapping files">
<hbm2java output="${source.generated}">
<fileset dir="${src.hibernate}">
<include name="**/*.hbm.xml"/>
</fileset>
</hbm2java>
</target>