How do I use org.hibernate.tool.ant.HibernateToolTask as an alternative to org.hibernate.tool.hbm2ddl.SchemaExportTask?

Hi team,

Currently, my project is using xdoclet (2.0.1) and hibernate (4.3.10) to generate *.hbm.xml, and export SQL. I have tried to upgrade these libraries to the new version, especially for Hibernate to version 5.6.

But with old configuration:

<macrodef name="create.db.schema">
        <attribute name="dbtype"/>
        <attribute name="properties"/>
        <attribute name="outfile"/>
        <attribute name="mappingdir"/>
        <sequential>
            <taskdef 
                name="schemaexport" 
                classname="org.hibernate.tool.hbm2ddl.SchemaExportTask" 
                classpathref="classpath" 
            />
            <echo message="Generating Hibernate Schema for @{dbtype} to @{outfile} using @{properties}"/>
            <schemaexport 
                properties="@{properties}" 
                quiet="yes" 
                text="yes" 
                drop="no" 
                delimiter=";" 
                output="@{outfile}">
                <fileset dir="@{mappingdir}">
                    <include name="**/*.hbm.xml" /> 
                </fileset>
            </schemaexport>
        </sequential>
     </macrodef>

I can’t generate DDL to *hbm.xml with the error “Error performing export: Could not parse mapping document: D:…\model\AclTemplate.hbm.xml (FILE)”. I try Hibernate-Tool with the following configuration:

<macrodef name="create.db.schema">
	  <attribute name="dbtype"/>
	  <attribute name="properties"/>
	  <attribute name="outfile"/>
	  <attribute name="mappingdir"/>
	  <sequential>
	    <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="classpath"/>
	    <echo message="Generating Hibernate Schema for @{dbtype} to @{outfile} using @{properties}"/>
	    <hibernatetool destdir="temp">
	      <classpath refid="classpath"/>
	      <configuration configurationfile="conf\hibernate.cfg.xml">
	        <fileset dir="@{mappingdir}">
	          <include name="**/*.hbm.xml"/>
	        </fileset>
	      </configuration>
	      <hbm2ddl export="true" outputfilename="table.sql" drop="true" delimiter=";"/>
	    </hibernatetool>
	  	<move file="temp/table.sql" tofile="@{outfile}" overwrite="true"/>
	  	<delete dir="temp"/>
	  </sequential>
	</macrodef>

Before, I used the hibernate.properties file, and now I use hibernate.cfg.xml to configure, but the content is the same as before. It generates success. But it makes some commands like “alter table” disappear, so I checked and saw that it would export the final foreign key of file *hbm.xml. For example, in file table2.hbm.xml include:

<subclass name="com.example.Class1" dynamic-insert="true" discriminator-value="NAME_CLASS1" dynamic-update="true">
  <many-to-one foreign-key="FK_CLASS1" class="com.example.Model1" fetch="join" name="className">
	<column name="table1_id"/>
  </many-to-one>
</subclass>
<subclass name="com.example.Class2" dynamic-insert="true" discriminator-value="NAME_CLASS2" dynamic-update="true">
  <many-to-one foreign-key="FK_CLASS2" class="com.example.Model2" fetch="join" name="className">
	<column name="table1_id"/>
  </many-to-one>
</subclass>
<subclass name="com.example.Class3" dynamic-insert="true" discriminator-value="NAME_CLASS3" dynamic-update="true">
  <many-to-one foreign-key="FK_CLASS3" class="com.example.Model3" fetch="join" name="className">
	<column name="table1_id"/>
  </many-to-one>
</subclass>

With Hibernate 4.3.10 and the old configuration, it will generate three commands as follows:

–alter table table2 add constraint FK_CLASS1 foreign key (table1_id) references table1

alter table table2 add constraint FK_CLASS2 foreign key (table1_id) references table1

–alter table table2 add constraint FK_CLASS3 foreign key (table1_id) references table1

but now it only generates:

–alter table table2 add constraint FK_CLASS3 foreign key (table1_id) references table1

I tried dropping foreign key FK_CLASS3 and it generated the foreign key FK_CLASS 2.

Do you know any configuration for HibernateToolTask to generate like before?

Or

Do you know any way to use SchemaExportTask to generate the same as Hibernate 4.3.10?

Thanks
Phong Huynh