<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    【永恒的瞬間】
    ?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 閱讀(1097) 評論(0)  編輯  收藏 所屬分類: hibernate
    主站蜘蛛池模板: 亚洲免费视频网站| 一级毛片高清免费播放| 久久永久免费人妻精品| 亚洲V无码一区二区三区四区观看| 国产精品无码免费专区午夜| 亚洲精品成人无限看| 久久国产免费一区二区三区| 亚洲精品免费观看| 91精品国产免费| 亚洲香蕉久久一区二区| 日韩免费福利视频| 免费人成视频在线观看免费| 中文字幕亚洲乱码熟女一区二区| A毛片毛片看免费| 久久精品蜜芽亚洲国产AV| 性短视频在线观看免费不卡流畅| 欧洲 亚洲 国产图片综合| 日本一道高清不卡免费| 国产精品内射视频免费| 久久久久亚洲AV无码专区体验 | 黄色片免费在线观看| 久久亚洲AV无码精品色午夜麻| 99re6在线视频精品免费下载| xxx毛茸茸的亚洲| 婷婷综合缴情亚洲狠狠尤物| 免费无码av片在线观看| 亚洲国产午夜精品理论片| 国产一区二区三区在线观看免费| 男女拍拍拍免费视频网站| 亚洲视频免费一区| 国产成人啪精品视频免费网| 九九精品成人免费国产片| 亚洲AV综合色区无码二区偷拍 | 性做久久久久免费观看| 成人免费网站视频www| 久久精品国产亚洲AV大全| 国产精品久久免费视频| 99re6在线精品视频免费播放 | 在线a亚洲v天堂网2019无码| 亚洲免费视频观看| 四虎影视久久久免费观看|