How to add hbm2java task to Ant in NetBeans

2.6k views Asked by At

I'm trying to generate POJO's from mapping xml files. I read something about adding an ant task, to do it easily.

I've added this xml below to my project's build-impl.xml in Netbeans, but nothing happens:

<target name="codegen">
     <echo>Zippzip</echo>
    <taskdef name="hbm2java"
             classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
             classpathref="javac.classpath"/>
    <hbm2java 
              output="generated/src/">
        <fileset dir="cat/model/">
            <include name="**/*.hbm.xml"/>
        </fileset>
    </hbm2java>
</target>

I'm beginner to Netbeans, Ant and Hibernate, can anyone help me out?

P.S. i don't really know what should 'classpathref' be. I mean i know it should contain the classpath to the hibernate's distribution. The real problem is that I don't know how to get an Ant task working..

Edit: I figured out that the script above doesn't work with Hibernate3.. I've got another script, but still not working. The error message shown is: Could not create type hibernatetool as the class class org.hibernate.tool.ant.Hbm2JavaExporterTask has no compatible constructor; And the script:

<target name="codegen">
    <taskdef name="hibernatetool"
        classname="org.hibernate.tool.ant.Hbm2JavaExporterTask">
        <classpath refid="project.classpath" />
    </taskdef>

    <hibernatetool destdir="cat/model/">
        <configuration configurationfile="hibernate.cfg.xml"/>
        <hbm2java />
    </hibernatetool>
</target>

This is Hibernate3 compatible, as I saw in the hibernate docs: http://docs.jboss.org/tools/2.1.0.Beta1/hibernatetools/html/ant.html#d0e2903

4

There are 4 answers

1
omermuhammed On BEST ANSWER

I dont know anything about hbm2java but after adding a task like in the code above, you need to add the jars associated with this to Ant. This is done by copying the jar file inton $ANT_HOME/lib directory. Did you do this?

0
Janov Byrnisson On

Figured out: should replace 'org.hibernate.tool.ant.Hbm2JavaExporterTask' with 'org.hibernate.tool.ant.HibernateToolTask'

0
Gord On

This is how I use it.

    <hibernatetool destdir="${src.java.dir}">
        <configuration>
            <fileset dir="${src.java.dir}">
                <include name="**/*.hbm.xml"/>
            </fileset>
        </configuration>
        <hbm2java/> <!-- Generate POJO source -->
    </hibernatetool>

</target>
0
Lefteris Laskaridis On

Have you considered using jpa annotations instead of xml?