<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任務(wù)來進行與Hibernate關(guān)聯(lián)的轉(zhuǎn)換。

    除了Ant任務(wù)外,當前的Hibernate Tools也包含了Eclipse IDE的插件,用于與現(xiàn)存數(shù)據(jù)庫的逆向工程。

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

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

    • Development Wizards: 在Hibernate Eclipse tools中還提供了幾個向?qū)?;你可以用向?qū)Э焖偕蒆ibernate 配置文件(cfg.xml),你甚至還可以同現(xiàn)存的數(shù)據(jù)庫schema中反向工程出POJO源代碼與Hibernate 映射文件。反向工程支持可定制的模版。

    • Ant Tasks:

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

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

    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約束檢查

    21.1.2. 運行該工具

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

    21.1.3. 屬性(Properties)

    可以通過如下方式指定數(shù)據(jù)庫屬性:

    • 通過-D<property>系統(tǒng)參數(shù)

    • hibernate.properties文件中

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

    所需的參數(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)

    21.1.4. 使用Ant(Using Ant)

    你可以在你的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>
    posted on 2007-05-15 16:01 ???MengChuChen 閱讀(1097) 評論(0)  編輯  收藏 所屬分類: hibernate
    主站蜘蛛池模板: 亚洲精华国产精华精华液好用| 亚洲精品日韩中文字幕久久久| 理论亚洲区美一区二区三区| 成人人免费夜夜视频观看| 亚洲国产成人精品久久| 九九精品免费视频| 精品久久亚洲中文无码| 歪歪漫画在线观看官网免费阅读| 亚洲a级成人片在线观看| 成年人网站在线免费观看| 亚洲欧美日韩中文无线码| 免费国产一级特黄久久| 免费在线人人电影网| 久久精品亚洲男人的天堂| www免费黄色网| 久久国产亚洲观看| 最近免费最新高清中文字幕韩国| 亚洲系列中文字幕| 午夜免费福利在线| 五月天婷婷免费视频| 亚洲精品国产精品乱码不卡√| 男人都懂www深夜免费网站| 亚洲日韩乱码久久久久久| 亚洲精品动漫免费二区| 美女裸体无遮挡免费视频网站| 亚洲综合伊人久久综合| 99在线免费观看视频| 日韩亚洲人成在线| 国产成人A亚洲精V品无码| 久久综合国产乱子伦精品免费| 亚洲成人福利在线| 免费看小12萝裸体视频国产| 老司机精品免费视频| 久久久亚洲AV波多野结衣| 免费鲁丝片一级观看| 中国黄色免费网站| 日本亚洲精品色婷婷在线影院| 国产精品嫩草影院免费| 精品免费视在线观看| 亚洲性无码一区二区三区| 国产偷国产偷亚洲高清日韩|