可以通過一系列Eclipse插件、命令行工具和Ant任務(wù)來進(jìn)行與Hibernate關(guān)聯(lián)的轉(zhuǎn)換。
除了Ant任務(wù)外,當(dāng)前的Hibernate Tools也包含了Eclipse IDE的插件,用于與現(xiàn)存數(shù)據(jù)庫的逆向工程。
-
Mapping Editor: Hibernate XML映射文件的編輯器,支持自動(dòng)完成和語法高亮。它也支持對類名和屬性/字段名的語義自動(dòng)完成,比通常的XML編輯器方便得多。
-
Console: Console是Eclipse的一個(gè)新視圖。除了對你的console配置的樹狀概覽,你還可以獲得對你持久化類及其關(guān)聯(lián)的交互式視圖。Console允許你對數(shù)據(jù)庫執(zhí)行HQL查詢,并直接在Eclipse中瀏覽結(jié)果。
-
Development Wizards: 在Hibernate Eclipse tools中還提供了幾個(gè)向?qū)В荒憧梢杂孟驅(qū)Э焖偕蒆ibernate 配置文件(cfg.xml),你甚至還可以同現(xiàn)存的數(shù)據(jù)庫schema中反向工程出POJO源代碼與Hibernate 映射文件。反向工程支持可定制的模版。
-
Ant Tasks:
要得到更多信息,請查閱 Hibernate Tools 包及其文檔。
同時(shí),Hibernate主發(fā)行包還附帶了一個(gè)集成的工具(它甚至可以在Hibernate“內(nèi)部”快速運(yùn)行)SchemaExport ,也就是 hbm2ddl。
可以從你的映射文件使用一個(gè)Hibernate工具生成DDL。 生成的schema包含有對實(shí)體和集合類表的完整性引用約束(主鍵和外鍵)。涉及到的標(biāo)示符生成器所需的表和sequence也會(huì)同時(shí)生成。
在使用這個(gè)工具的時(shí)候,你必須 通過hibernate.dialet屬性指定一個(gè)SQL方言(Dialet),因?yàn)镈DL是與供應(yīng)商高度相關(guān)的。
首先,要定制你的映射文件,來改善生成的schema。
很多Hibernate映射元素定義了一個(gè)可選的length屬性。你可以通過這個(gè)屬性設(shè)置字段的長度。 (如果是Or, for numeric/decimal data types, the precision.)
有些tag接受not-null屬性(用來在表字段上生成NOT NULL約束)和unique屬性(用來在表字段上生成UNIQUE約束)。
有些tag接受index屬性,用來指定字段的index名字。unique-key屬性可以對成組的字段指定一個(gè)組合鍵約束(unit key constraint)。目前,unique-key屬性指定的值并不會(huì)被當(dāng)作這個(gè)約束的名字,它們只是在用來在映射文件內(nèi)部用作區(qū)分的。
示例:
<property name="foo" type="string" length="64" not-null="true"/> <many-to-one name="bar" foreign-key="fk_foo_bar" not-null="true"/> <element column="serial_number" type="long" not-null="true" unique="true"/>
另外,這些元素還接受<column>子元素。在定義跨越多字段的類型時(shí)特別有用。
<property name="foo" type="string"> <column name="foo" length="64" not-null="true" sql-type="text"/> </property> <property name="bar" type="my.customtypes.MultiColumnType"/> <column name="fee" not-null="true" index="bar_idx"/> <column name="fi" not-null="true" index="bar_idx"/> <column name="fo" not-null="true" index="bar_idx"/> </property>
sql-type屬性允許用戶覆蓋默認(rèn)的Hibernate類型到SQL數(shù)據(jù)類型的映射。
check屬性允許用戶指定一個(gè)約束檢查。
<property name="foo" type="integer"> <column name="foo" check="foo > 10"/> </property> <class name="Foo" table="foos" check="bar < 100.0"> ... <property name="bar" type="float"/> </class>
表 21.1. Summary
屬性(Attribute) | 值(Values) | 解釋(Interpretation) |
---|---|---|
length | 數(shù)字 | 字段長度/小數(shù)點(diǎn)精度 |
not-null | true|false | 指明字段是否應(yīng)該是非空的 |
unique | true|false | 指明是否該字段具有惟一約束 |
index | index_name | 指明一個(gè)(多字段)的索引(index)的名字 |
unique-key | unique_key_name | 指明多字段惟一約束的名字(參見上面的說明) |
foreign-key | foreign_key_name | 指明一個(gè)外鍵的名字,它是為關(guān)聯(lián)生成的。 |
sql-type | column_type | 覆蓋默認(rèn)的字段類型(只能用于<column>屬性) |
check | SQL 表達(dá)式 | 對字段或表加入SQL約束檢查 |
SchemaExport工具把DDL腳本寫到標(biāo)準(zhǔn)輸出,同時(shí)/或者執(zhí)行DDL語句。
java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaExport options mapping_files
表 21.2. SchemaExport命令行選項(xiàng)
選項(xiàng) | 說明 |
---|---|
--quiet | 不要把腳本輸出到stdout |
--drop | 只進(jìn)行drop tables的步驟 |
--text | 不執(zhí)行在數(shù)據(jù)庫中運(yùn)行的步驟 |
--output=my_schema.ddl | 把輸出的ddl腳本輸出到一個(gè)文件 |
--config=hibernate.cfg.xml | 從XML文件讀入Hibernate配置 |
--properties=hibernate.properties | 從文件讀入數(shù)據(jù)庫屬性 |
--format | 把腳本中的SQL語句對齊和美化 |
--delimiter=x | 為腳本設(shè)置行結(jié)束符 |
你甚至可以在你的應(yīng)用程序中嵌入SchemaExport工具:
Configuration cfg = ....; new SchemaExport(cfg).create(false, true);
可以通過如下方式指定數(shù)據(jù)庫屬性:
-
通過-D<property>系統(tǒng)參數(shù)
-
在hibernate.properties文件中
-
位于一個(gè)其它名字的properties文件中,然后用 --properties參數(shù)指定
所需的參數(shù)包括:
你可以在你的Ant build腳本中調(diào)用SchemaExport:
<target name="schemaexport"> <taskdef name="schemaexport" classname="org.hibernate.tool.hbm2ddl.SchemaExportTask" classpathref="class.path"/> <schemaexport properties="hibernate.properties" quiet="no" text="no" drop="no" delimiter=";" output="schema-export.sql"> <fileset dir="src"> <include name="**/*.hbm.xml"/> </fileset> </schemaexport> </target>
SchemaUpdate工具對已存在的schema采用"增量"方式進(jìn)行更新。注意SchemaUpdate嚴(yán)重依賴于JDBC metadata API,所以它并非對所有JDBC驅(qū)動(dòng)都有效。
java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaUpdate options mapping_files
表 21.4. SchemaUpdate命令行選項(xiàng)
選項(xiàng) | 說明 |
---|---|
--quiet | 不要把腳本輸出到stdout |
--properties=hibernate.properties | 從指定文件讀入數(shù)據(jù)庫屬性 |
你可以在你的應(yīng)用程序中嵌入SchemaUpdate工具:
Configuration cfg = ....; new SchemaUpdate(cfg).execute(false);
你可以在Ant腳本中調(diào)用SchemaUpdate:
<target name="schemaupdate"> <taskdef name="schemaupdate" classname="org.hibernate.tool.hbm2ddl.SchemaUpdateTask" classpathref="class.path"/> <schemaupdate properties="hibernate.properties" quiet="no"> <fileset dir="src"> <include name="**/*.hbm.xml"/> </fileset> </schemaupdate> </target>