Пытаюсь усиленно освоить Hibernate Annotations, столкнулся со проблемой экспорта ddl-скрипта, на основании объектной модели использующие аннотации. Раньше формировал непосредственно *.hbm.xml – маппинг-файлы сгенерированные через XDoclet. И используя в Ant класс net.sf.hibernate.tool.hbm2ddl.SchemaExportTask удачно производил экспорт в ddl-схемы в файл.

Пример экспорта ddl-схемы на основе *.hbm.xml – маппинг-файлов:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<target name="ddl.generate"> 
	<path id="hibernate.export.classpath"> 
		<fileset dir="lib/"> 
			<include name="*.jar" /> 
		</fileset> 
	</path> 
	<taskdef name="schemaexport" classname="org.hibernate.tool.hbm2ddl.SchemaExportTask" 
		classpathref="hibernate.export.classpath" mce_href="hibernate.export.classpath" /> 
	<schemaexport properties="../hibernate.properties" text="yes" drop="no" delimiter=";" output="ddl-schema.sql"> 
		<fileset dir="${hibernate.mapping.dir}"> 
			<include name="**/*.hbm.xml"/> 
		</fileset> 
	</schemaexport> 
</target>

Взяв на вооружение Hibernate Annotations, задался целью добиться аналогичного результата. Решением явилось использование класса org.hibernate.tool.ant.HibernateToolTask из библиотеки hibernate-tools.jar.

Вот пример экспорта ddl-схемы на основе аннотированных классов, находящихся в папке ${targetdir}:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<target name="ddl.generate"> 
	<path id="hibernate.export.classpath"> 
		<fileset dir="lib/"> 
			<include name="*.jar" /> 
		</fileset> 
	</path> 
	<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" 
		classpathref="hibernate.export.classpath" mce_href="hibernate.export.classpath" /> 
	<hibernatetool destdir="${targetdir}"> 
		<classpath> 
			<path location="${targetdir}" /> 
		</classpath> 
		<annotationconfiguration configurationfile="bin/hibernate.cfg.xml" /> 
		<hbm2ddl export="true" create="true" delimiter=";" format="true" outputfilename="ddl-schema.sql" /> 
	</hibernatetool> 
</target>

Для поиска данного решения помогли следующие ссылки: