| 工具 | 輸入 | 輸出 | 典型場景 |
|---|---|---|---|
| hbm2java | 映射檔 (*.hbm.xml) | Java 實體類別 | 資料庫優先,生成 Domain Model |
| hbm2ddl | 映射檔 | DDL 腳本 (建表 SQL) | 模型優先,自動建立資料庫結構 |
| XDoclet | 帶註解的 Java 原始碼 | 映射檔 + 實體類別 + DDL + DAO 等 | 代碼優先,註解驅動全鏈路生成 |
| Middlegen | 資料庫 Schema (JDBC 中繼資料) | 映射檔 + 實體類別 + EJB 等 | 現有資料庫逆向工程 |
<class> 類別層級屬性<class name="com.myapp.Customer"
table="CUSTOMER"
discriminator-value="CUST"
dynamic-insert="true"
dynamic-update="true"
select-before-update="false"
polymorphism="implicit"
batch-size="10"
optimistic-lock="version"
lazy="true">
<!-- 類別描述 (生成 JavaDoc) -->
<meta attribute="class-description">客戶領域模型</meta>
<!-- 繼承父類 -->
<meta attribute="extends">com.myapp.BaseEntity</meta>
<!-- 實作介面 -->
<meta attribute="implements">java.io.Serializable, com.myapp.Auditable</meta>
<!-- 生成類別修飾符 -->
<meta attribute="scope-class">public</meta>
<!-- toString() 包含屬性 -->
<meta attribute="use-in-tostring">true</meta>
</class>
<id> 識別符生成策略<id name="id" type="long" column="CUST_ID">
<meta attribute="scope-field">protected</meta> <!-- 欄位修飾符 -->
<meta attribute="scope-get">public</meta> <!-- getter 修飾符 -->
<meta attribute="scope-set">protected</meta> <!-- setter 修飾符 -->
<generator class="native"></generator> <!-- identity/sequence/hilo/uuid 等 -->
</id>
<property> 一般屬性完整配置<property name="email" type="string" column="EMAIL"
not-null="true" unique="true" length="100">
<meta attribute="field-description">客戶電子郵件,唯一索引</meta>
<meta attribute="use-in-tostring">true</meta>
<meta attribute="gen-property">true</meta> <!-- 是否生成欄位/存取器 -->
<column name="EMAIL"
sql-type="varchar(100)"
not-null="true"
unique="true"
index="idx_customer_email"
check="email LIKE '%@%'">
</column>
</property>
<set name="orders" table="ORDERS" lazy="true" inverse="true" cascade="all-delete-orphan">
<key column="CUST_ID" not-null="true"></key>
<one-to-many class="com.myapp.Order"/>
<meta attribute="use-in-tostring">false</meta> <!-- 避免遞迴 toString -->
</set>
<project name="hibernate-codegen" default="generate" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<property name="hbm.dir" value="src/main/resources/hbm"/>
<property name="gen.src" value="target/generated-sources/java"/>
<property name="gen.sql" value="target/generated-sources/sql"/>
<target name="generate" depends="hbm2java, hbm2ddl"/>
<!-- hbm2java -->
<target name="hbm2java">
<taskdef name="hbm2java" classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask" classpathref="hibernate.classpath"/>
<hbm2java output="${gen.src}">
<fileset dir="${hbm.dir}" includes="**/*.hbm.xml"/>
</hbm2java>
</target>
<!-- hbm2ddl (產出 schema.sql) -->
<target name="hbm2ddl">
<taskdef name="schemaexport" classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask" classpathref="hibernate.classpath"/>
<schemaexport
properties="hibernate.properties"
quiet="no"
text="yes"
drop="no"
create="yes"
output="${gen.sql}/schema.sql"
delimiter=";"
format="yes">
<fileset dir="${hbm.dir}" includes="**/*.hbm.xml"/>
</schemaexport>
</target>
</project>
@Entity 註解取代 .hbm.xml,hibernate.hbm2ddl.auto=update/validate 替代 hbm2ddlhbm2java/hbm2ddl/XDoclet/Middlegen 曾是 Hibernate 生態的「代碼生成四大金剛」,解決了早期大量樣板碼痛點。隨著註解驅動、遷移工具、編譯期生成技術成熟,現代架構已轉向源碼即真理、版本化遷移、編譯期驗證。理解工具鏈演進脈絡,有助於在遷移舊專案時做出合理技術決策。