【永恒的瞬間】
          ?Give me hapy ?

          第 21 章 工具箱指南

          可以通過一系列Eclipse插件、命令行工具和Ant任務來進行與Hibernate關聯的轉換。

          除了Ant任務外,當前的Hibernate Tools也包含了Eclipse IDE的插件,用于與現存數據庫的逆向工程。

          • Mapping Editor: Hibernate XML映射文件的編輯器,支持自動完成和語法高亮。它也支持對類名和屬性/字段名的語義自動完成,比通常的XML編輯器方便得多。

          • Console: Console是Eclipse的一個新視圖。除了對你的console配置的樹狀概覽,你還可以獲得對你持久化類及其關聯的交互式視圖。Console允許你對數據庫執行HQL查詢,并直接在Eclipse中瀏覽結果。

          • Development Wizards: 在Hibernate Eclipse tools中還提供了幾個向導;你可以用向導快速生成Hibernate 配置文件(cfg.xml),你甚至還可以同現存的數據庫schema中反向工程出POJO源代碼與Hibernate 映射文件。反向工程支持可定制的模版。

          • Ant Tasks:

          要得到更多信息,請查閱 Hibernate Tools 包及其文檔。

          同時,Hibernate主發行包還附帶了一個集成的工具(它甚至可以在Hibernate“內部”快速運行)SchemaExport ,也就是 hbm2ddl

          21.1. Schema自動生成(Automatic schema generation)

          可以從你的映射文件使用一個Hibernate工具生成DDL。 生成的schema包含有對實體和集合類表的完整性引用約束(主鍵和外鍵)。涉及到的標示符生成器所需的表和sequence也會同時生成。

          在使用這個工具的時候,你必須 通過hibernate.dialet屬性指定一個SQL方言(Dialet),因為DDL是與供應商高度相關的。

          首先,要定制你的映射文件,來改善生成的schema。

          21.1.1. 對schema定制化(Customizing the schema)

          很多Hibernate映射元素定義了一個可選的length屬性。你可以通過這個屬性設置字段的長度。 (如果是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屬性指定的值并不會被當作這個約束的名字,它們只是在用來在映射文件內部用作區分的。

          示例:

          <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數據類型的映射。

          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 數字 字段長度/小數點精度
          not-null true|false 指明字段是否應該是非空的
          unique true|false 指明是否該字段具有惟一約束
          index index_name 指明一個(多字段)的索引(index)的名字
          unique-key unique_key_name 指明多字段惟一約束的名字(參見上面的說明)
          foreign-key foreign_key_name 指明一個外鍵的名字,它是為關聯生成的。
          sql-type column_type 覆蓋默認的字段類型(只能用于<column>屬性)
          check SQL 表達式 對字段或表加入SQL約束檢查

          21.1.2. 運行該工具

          SchemaExport工具把DDL腳本寫到標準輸出,同時/或者執行DDL語句。

          java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaExport options mapping_files

          表 21.2. SchemaExport命令行選項

          選項 說明
          --quiet 不要把腳本輸出到stdout
          --drop 只進行drop tables的步驟
          --text 不執行在數據庫中運行的步驟
          --output=my_schema.ddl 把輸出的ddl腳本輸出到一個文件
          --config=hibernate.cfg.xml 從XML文件讀入Hibernate配置
          --properties=hibernate.properties 從文件讀入數據庫屬性
          --format 把腳本中的SQL語句對齊和美化
          --delimiter=x 為腳本設置行結束符

          你甚至可以在你的應用程序中嵌入SchemaExport工具:

          Configuration cfg = ....;
          new SchemaExport(cfg).create(false, true);

          21.1.3. 屬性(Properties)

          可以通過如下方式指定數據庫屬性:

          • 通過-D<property>系統參數

          • hibernate.properties文件中

          • 位于一個其它名字的properties文件中,然后用 --properties參數指定

          所需的參數包括:

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

          21.1.4. 使用Ant(Using Ant)

          你可以在你的Ant build腳本中調用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驅動都有效。

          java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaUpdate options mapping_files

          表 21.4. SchemaUpdate命令行選項

          選項 說明
          --quiet 不要把腳本輸出到stdout
          --properties=hibernate.properties 從指定文件讀入數據庫屬性

          你可以在你的應用程序中嵌入SchemaUpdate工具:

          Configuration cfg = ....;
          new SchemaUpdate(cfg).execute(false);

          21.1.6. 用Ant來增量更新schema(Using Ant for incremental schema updates)

          你可以在Ant腳本中調用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>
          posted on 2007-05-15 16:01 ???MengChuChen 閱讀(1099) 評論(0)  編輯  收藏 所屬分類: hibernate
          主站蜘蛛池模板: 栾城县| 永嘉县| 汨罗市| 成安县| 阳高县| 三台县| 富川| 岑溪市| 呼伦贝尔市| 昆山市| 奉化市| 芒康县| 长武县| 青铜峡市| 故城县| 灵寿县| 静海县| 淳化县| 长沙县| 西峡县| 乾安县| 涿州市| 贵州省| 虞城县| 哈密市| 中牟县| 朝阳区| 平果县| 吴桥县| 武义县| 孟村| 东山县| 凯里市| 延长县| 涿州市| 东城区| 巨鹿县| 德令哈市| 集安市| 鄂州市| 平和县|