Ant Task結合Hibernate Tools 3.x快速開発
6734 ワード
最近Hibernate Referenceの文書を見た時、Hibernate Tools 3を発見しました.xという公式に推奨されているツールはもう強いです.以前はHibernateに有用な一般的にAnt Taskにxdoclet 2を直接結合して関連コードを生成するように開発されていたため,コードに良好な寸法があってこそ品質の優れたコードを生成することができ,容易ではなかった.開発プロセスによって我々の一般的なニーズを分析する場合、PowerDesignで議論しながらモデリングを開始し、最後に関連するDataBaseのSchemaを生成します.PDの強力な機能のため、それを使って各種DBのSchemaを生成するのは便利で効率的で、主に後でDBを変更する際に便利で、コードの質も悪くありません.もちろん、POJOを直接生成して個人の趣味を見ることもできます.
私の開発プロセスは一般的にPD CDMが主な構造を確定する(モデリング分析)----->PDMを生成し、必要に応じて調整する----->対応DBのSchema----->生成*.hbm.xml,POJO,cfg.xml----->状況に応じて微調整した後、DAO層-----を開始します...hibernate toolsと組み合わせてこのシリーズを完成するのはかなり楽です.しかし、チームごとに実際の状況が異なり、自分の状況に応じて最適な方法を選択します.以下、Ant Taskとhibernatetoolを組み合わせて説明します.
1.Ant構築環境
まずHibernate公式サイトに最新のHibenate Toolsをダウンロードして解凍した後に/plugins/org.hibernate.eclipse.x.x.x/lib/toolsディレクトリの下の*.JAr copyはANT構築環境にあります.e.g.
2.グローバルANT Taskの確立
実は一般的な用途では、このツールの中でという属性が最も主要です.他の複数の参照のためにグローバルなtaskに構成できます.具体的なフォーマットは次のとおりです.
そのうちの1つ以上を選ぶことができます.hibernatetoolプロパティは次の表に示されています.
属性名
定義#テイギ#
使用状況
destdir
ファイルの入力ディレクトリを生成
Required
templatepath
ユーザーがテンプレートを編集するパス
Optional
classpath
リソースの解析時の依存環境
Optionalですが、通常は要求です.
property(and propertyset)
出力のプロパティ設定を制御します.ほとんどは、ユーザー定義テンプレートのプロパティ提供に関連しています.
Optional
configuration (annotationconfiguration, jpaconfiguration, jdbcconfiguration)
Hibernateメタモデルの4つの属性の1つを指定する必要があります.
Required
hbm2java(hbm2cfgxml, hbmtemplate, etc.)
1つ以上の出力方法を指定
Required
次に例を示します.
注:以前のユーザー定義テンプレートはVelocityを使用していましたが、Freemarkerを使用し始めました.これにより、より良い例外とエラー処理が提供されます.
以下のように簡単に定義することもできます.
3.DBからマッピングファイル、POJOおよびプロファイルへのタスクの確立
前のグローバル定義があるため、次のタスクは、1つのtargetに定義することも、複数のtargetに分けて定義することもでき、実際のニーズを見ることができます.まずデータベースのプロパティファイルhibernateを定義します.properties
次に、関連するタスクを定義します.
公式ドキュメントには、Hibernate Tools Reference Guideを参照してください.
4.hibernateパッケージに付属のSchema生成ツール
例を挙げると、schemaの生成、ant taskの更新、検証、各パラメータの説明はHibernate Reference Toolset Guideを参照してください.
実はこれらの機能hibernate toolsもhbm 2 ddlのように備えていますが、みんなが好きなだけで、生成コードの品質は悪くありません.
私の開発プロセスは一般的にPD CDMが主な構造を確定する(モデリング分析)----->PDMを生成し、必要に応じて調整する----->対応DBのSchema----->生成*.hbm.xml,POJO,cfg.xml----->状況に応じて微調整した後、DAO層-----を開始します...hibernate toolsと組み合わせてこのシリーズを完成するのはかなり楽です.しかし、チームごとに実際の状況が異なり、自分の状況に応じて最適な方法を選択します.以下、Ant Taskとhibernatetoolを組み合わせて説明します.
1.Ant構築環境
まずHibernate公式サイトに最新のHibenate Toolsをダウンロードして解凍した後に/plugins/org.hibernate.eclipse.x.x.x/lib/toolsディレクトリの下の*.JAr copyはANT構築環境にあります.e.g.
<path id="toolslib">
<path location="lib/hibernate-tools.jar" />
<path location="lib/hibernate3.jar" />
<path location="lib/freemarker.jar" />
<path location="${jdbc.driver.jar}" />
</path>
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="toolslib" />
2.グローバルANT Taskの確立
実は一般的な用途では、このツールの中で
<hibernatetool
destdir="defaultDestinationDirectory"
templatepath="defaultTemplatePath">
<classpath ...>
<property key="propertyName" value="value"/>
<propertyset ...>
(<configuration ...>|<annotationconfiguration ...>|
<jpaconfiguration ...>|<jdbcconfiguration ...>)
(<hbm2java>,<hbm2cfgxml>,<hbmtemplate>,...)
</hibernatetool>
そのうちの1つ以上を選ぶことができます.hibernatetoolプロパティは次の表に示されています.
属性名
定義#テイギ#
使用状況
destdir
ファイルの入力ディレクトリを生成
Required
templatepath
ユーザーがテンプレートを編集するパス
Optional
classpath
リソースの解析時の依存環境
Optionalですが、通常は要求です.
property(and propertyset)
出力のプロパティ設定を制御します.ほとんどは、ユーザー定義テンプレートのプロパティ提供に関連しています.
Optional
configuration (annotationconfiguration, jpaconfiguration, jdbcconfiguration)
Hibernateメタモデルの4つの属性の1つを指定する必要があります.
Required
hbm2java(hbm2cfgxml, hbmtemplate, etc.)
1つ以上の出力方法を指定
Required
次に例を示します.
<hibernatetool destdir="${build.dir}/generated">
<classpath/>
<configuration configurationfile="hibernate.cfg.xml"/>
<!-- -->
<hbmtemplate exporterclass="my.own.Exporter" filepattern="."/>
</hibernatetool>
注:以前のユーザー定義テンプレートはVelocityを使用していましたが、Freemarkerを使用し始めました.これにより、より良い例外とエラー処理が提供されます.
以下のように簡単に定義することもできます.
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="classpath" />
3.DBからマッピングファイル、POJOおよびプロファイルへのタスクの確立
前のグローバル定義があるため、次のタスクは、1つのtargetに定義することも、複数のtargetに分けて定義することもでき、実際のニーズを見ることができます.まずデータベースのプロパティファイルhibernateを定義します.properties
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/vote?useUnicode=true&characterEncoding=utf8
hibernate.connection.username=root
hibernate.connection.password=junwei
hibernate.dialect=org.hibernate.dialect.MySQLDialect
次に、関連するタスクを定義します.
<target name="hibernatecode">
<!-- destdir , -->
<hibernatetool destdir="${build.dir}/generated">
<!-- packagename -->
<jdbcconfiguration propertyfile="hibernate.properties" packagename="sqe.janier.model"/>
<!-- DB hbm.xml -->
<hbm2hbmxml />
<!-- hbm.xml POJO -->
<hbm2java jdk5="true" />
<!-- -->
<hbm2cfgxml ejb3="false" />
<!-- POJO DAO , , -->
<hbm2dao />
</hibernatetool>
</target>
公式ドキュメントには、Hibernate Tools Reference Guideを参照してください.
4.hibernateパッケージに付属のSchema生成ツール
例を挙げると、schemaの生成、ant taskの更新、検証、各パラメータの説明はHibernate Reference Toolset Guideを参照してください.
<target name="schemaexport">
<taskdef name="schemaexport"
classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
classpathref="classpath" />
<schemaexport properties="hibernate.properties" quiet="no" text="no" drop="no"
delimiter=";" output="schema-export.sql">
<fileset dir="${src.dir}">
<include name="**/*.hbm.xml" />
</fileset>
</schemaexport>
</target>
<target name="schemaupdate">
<taskdef name="schemaupdate"
classname="org.hibernate.tool.hbm2ddl.SchemaUpdateTask"
classpathref="classpath" />
<schemaupdate properties="hibernate.properties" quiet="no">
<fileset dir="${src.dir}">
<include name="**/*.hbm.xml" />
</fileset>
</schemaupdate>
</target>
<target name="schemavalidate">
<taskdef name="schemavalidator"
classname="org.hibernate.tool.hbm2ddl.SchemaValidatorTask"
classpathref="classpath" />
<schemavalidator properties="hibernate.properties">
<fileset dir="${src.dir}">
<include name="**/*.hbm.xml" />
</fileset>
</schemavalidator>
</target>
実はこれらの機能hibernate toolsもhbm 2 ddlのように備えていますが、みんなが好きなだけで、生成コードの品質は悪くありません.