Tuesday, May 26, 2009

Hibernate's SchemaUpdateTask for Ant

I was trying to set up an Ant task to handle schema update from Hibernate (3.3.1 GA), something seemingly straight forward enough. But I kept getting NullPointerException. This annoys me to the extend that I googled for a whole day (yes, I am stubborn) trying to find a solution, but to a no avail.

Finally, I read the source code (should have done this way earlier) and realized the "outputFile" was never set anywhere and therefore it has to be specified in the Ant script. Sure enough, that got rid of the NullPointerException and the task was run successfully. And I am reasonably happy.

Here is how my task was defined:

  <target name="schemaupdate">
    <taskdef name="schemaupdate"
      classname="org.hibernate.tool.
                 hbm2ddl.SchemaUpdateTask"
      classpathref="project.class.path"/>
 
    <schemaupdate
      config="src/hibernate.cfg.xml"
      text="no"
      namingStrategy="org.hibernate.cfg.
                      DefaultNamingStrategy"
      outputFile="schema-update.sql"
      quiet="no">
    </schemaupdate>
  </task>

I couldn't find the existence or the need for the outputFile attribute documented anywhere. So I guess I do it here just in case there is another poor soul like me pulling all the hairs out just trying to find a solution like this. The "namingStrategy" there may not have been necessary, but I didn't feel like doing any further investigation (yes, I have a limit). Keeping it here wouldn't hurt, I guess.


2 comments:

loop said...

thank you!

jeff wang said...

I am really excited. I have already looked for the answer for all day. Thank for!