21.1. Schema自動生成(Automatic schema generation)
可以從你的映射文件使用一個Hibernate工具生成DDL。 生成的schema包含有對實體和集合類表的完整性引用約束(主鍵和外鍵)。涉及到的標示符生成器所需的表和sequence也會同時生成。
在使用這個工具的時候,你必須 通過hibernate.dialet屬性指定一個SQL方言(Dialet),因為DDL是與供應(yīng)商高度相關(guān)的。
首先,要定制你的映射文件,來改善生成的schema。
21.1.1. 對schema定制化(Customizing the schema)
很多Hibernate映射元素定義了一個可選的length屬性。你可以通過這個屬性設(shè)置字段的長度。 (如果是Or, for numeric/decimal data types, the precision.)
有些tag接受not-null屬性(用來在表字段上生成NOT NULL約束)和unique屬性(用來在表字段上生成UNIQUE約束)。
有些tag接受index屬性,用來指定字段的index名字。unique-key屬性可以對成組的字段指定一個組合鍵約束(unit key constraint)。目前,unique-key屬性指定的值并不會被當作這個約束的名字,它們只是在用來在映射文件內(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>子元素。在定義跨越多字段的類型時特別有用。
<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屬性允許用戶覆蓋默認的Hibernate類型到SQL數(shù)據(jù)類型的映射。
check屬性允許用戶指定一個約束檢查。
<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ù)點精度 |
not-null |
true|false |
指明字段是否應(yīng)該是非空的 |
unique |
true|false |
指明是否該字段具有惟一約束 |
index |
index_name |
指明一個(多字段)的索引(index)的名字 |
unique-key |
unique_key_name |
指明多字段惟一約束的名字(參見上面的說明) |
foreign-key |
foreign_key_name |
指明一個外鍵的名字,它是為關(guān)聯(lián)生成的。 |
sql-type |
column_type |
覆蓋默認的字段類型(只能用于<column>屬性) |
check |
SQL 表達式 |
對字段或表加入SQL約束檢查 |
SchemaExport工具把DDL腳本寫到標準輸出,同時/或者執(zhí)行DDL語句。
java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaExport options mapping_files
表 21.2. SchemaExport命令行選項
選項 |
說明 |
--quiet |
不要把腳本輸出到stdout |
--drop |
只進行drop tables的步驟 |
--text |
不執(zhí)行在數(shù)據(jù)庫中運行的步驟 |
--output=my_schema.ddl |
把輸出的ddl腳本輸出到一個文件 |
--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ù)庫屬性:
所需的參數(shù)包括:
表 21.3. SchemaExport 連接屬性
屬性名 |
說明 |
hibernate.connection.driver_class |
jdbc driver class |
hibernate.connection.url |
jdbc url |
hibernate.connection.username |
database user |
hibernate.connection.password |
user password |
hibernate.dialect |
方言(dialect) |
你可以在你的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>
21.1.5. 對schema的增量更新(Incremental schema updates)
SchemaUpdate工具對已存在的schema采用"增量"方式進行更新。注意SchemaUpdate嚴重依賴于JDBC metadata API,所以它并非對所有JDBC驅(qū)動都有效。
java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaUpdate options mapping_files
表 21.4. SchemaUpdate命令行選項
選項 |
說明 |
--quiet |
不要把腳本輸出到stdout |
--properties=hibernate.properties |
從指定文件讀入數(shù)據(jù)庫屬性 |
你可以在你的應(yīng)用程序中嵌入SchemaUpdate工具:
Configuration cfg = ....;
new SchemaUpdate(cfg).execute(false);
21.1.6. 用Ant來增量更新schema(Using Ant for incremental schema updates)
你可以在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>