samsonych.com

Shine On You Crazy Diamond!

Экспорт ddl схемы. Ant. Hibernate Annotations

22nd February 2008

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

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

<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}:

<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>

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

One Response to “Экспорт ddl схемы. Ant. Hibernate Annotations”

  1. Stepan Says:

    Да уж. В этом блоге хоть комментаторы вменяемые А то пишут обычно в комментарии чушь всякую.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>