Hibernate Class Generation Using hbm2java

Hibernate Class Generation Using hbm2java

Customizing the Build Process with Maven 1

To integrate this into a Maven (1.0) build process, you need to modify your maven.xml file. The Maven code is presented here. The script basically checks whether the Hibernate mapping files have been changed since the last time the classes were generated (using the uptodate tag), and, if not, invokes the Ant hbm2java task described earlier. In this case, the following assumptions are made:



  • The hbm2java.xml configuration file is expected to be in the src/hibernate directory.
  • The Hibernate mapping files are expected to be in the src/hibernate directory.
  • The Java classes will be generated in the src/generated/src/java directory.

  

<goal name="generate-hibernate-classes">

    <ant:echo message="Hibernate class generation"/>



    <fileset dir="${basedir}/src" id="fileset.hbm.xml">

        <include name="**/*.hbm.xml"/>

    </fileset>



    <uptodate property="hibernateBuild.uptodate"

              targetfile="${maven.src.dir}/generated/hbm.jar">

       <srcfiles refid="fileset.hbm.xml"/>

    </uptodate>



    <j:set var="buildHibernateFiles"

           value="${hibernateBuild.uptodate}"/>



    <j:choose>

        <j:when test="${buildHibernateFiles != null}">

              <ant:echo message="Hibernate classes up to date"/>

        </j:when>

        <j:otherwise>

              <ant:echo message="Generating Hibernate classes to src/java"/>



              <ant:taskdef name="hbm2java"

                           classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"

                           classpathref="maven.dependency.classpath"/>



              <ant:hbm2java config="${maven.src.dir}/conf/hbm2java.xml"

                            output="${maven.src.dir}/generated/src/java" >

                  <ant:fileset dir="${maven.src.dir}/hibernate">

                      <ant:include name="**/*.hbm.xml"/>

                  </ant:fileset>

              </ant:hbm2java>



              <ant:jar jarfile="${maven.src.dir}/generated/hbm.jar">

                  <fileset refid="fileset.hbm.xml"/>

              </ant:jar>

        </j:otherwise>

    </j:choose>

</goal>

  

 

Customizing the Build Process with Maven 2

If you happen to be using Maven 2, things are a little simpler. Instead of using pre and post goals in the maven.xml file, you add a maven-antrun-plugin plugin to the pom.xml file. Within this plugin, in the tasks section, you can directly invoke the Ant tasks, described above.


  

<project...>

  <modelVersion>4.0.0</modelVersion>

  ...

  <build>

    ...

    <plugins>

       <plugin>

          <artifactId>maven-antrun-plugin</artifactId>

          <executions>

            <execution>

              <phase>generate-sources</phase>

              <configuration>

                <tasks>

                  <taskdef name="hibernatetool"

                           classname="org.hibernate.tool.ant.HibernateToolTask"

                           classpathref="maven.dependency.classpath"/>



                  <hbm2java output="src/generated">

                      <fileset dir="src/hibernate">

                          <include name="**/*.hbm.xml"/>

                      </fileset>

                  </hbm2java>

                </tasks>

              </configuration>

              <goals>

                <goal>run</goal>

              </goals>

            </execution>

          </executions>

        </plugin>

     </plugins>

  </build>

</project>

  

Prev  [1] [2] [3] [4] [5] [6] Next

Close    To Top
  • Prev Article-Java:
  • Next Article-Java:
  • Now: Tutorial for Web and Software Design > Java > JDOnJDBCnSQLJ > Java Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction