隨筆 - 63  文章 - 0  trackbacks - 0
          <2009年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          在您撰寫好*.hbm.xml映射文件之後,您可以使用 net.sf.hibernate.tool.hbm2ddl.SchemaExportTask來自動建立資料庫表格,這邊所使用的方式是結合Ant進行自動化建構,首先我們假設將使用以下的User.hbm.xml:

          User.hbm.xml
          <?xml version="1.0"?>
          <!DOCTYPE hibernate-mapping
          PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
              "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
          
          <hibernate-mapping>
          <class name="onlyfun.caterpillar.User" table="USER">
          <id name="id" type="string" unsaved-value="null">
          <column name="user_id" sql-type="char(32)"/>
          <generator class="uuid.hex"/>
          </id>
          <property name="name" type="string" not-null="true">
          <column name="name" length="16" not-null="true"/>
          </property>
          <property name="sex" type="char" />
          <property name="age" type="int"/>
          </class>
          </hibernate-mapping>

           在這個映射文件中,<column/>標籤用於指定建立表格時的一些資訊,例如映射的表格欄位名稱,或是sql-type或 length等屬性,如果不指定這些資訊時,SchemaExportTask將自動使用Hibernate的類型至SQL類型等資訊來建立表格;sql -type用於指定表格欄位型態,not-null表示欄位不能為null,length則用於指定表格文字欄位長度,這些屬性的說明,都可以在 Hibernate參考手冊的表15.1找到。

           下面的build.xml用於Ant自動化建構時,生成資料庫表格之用:

          build.xml
          <project name="Hibernate" default="schema" basedir=".">
          <property name="source.root" value="src"/>
          <property name="class.root" value="classes"/>
          <property name="lib.dir" value="lib"/>
          <property name="data.dir" value="data"/>
          <path id="project.class.path">
          <!-- Include our own classes, of course -->
          <pathelement location="${class.root}" />
          <!-- Include jars in the project library directory -->
          <fileset dir="${lib.dir}">
          <include name="*.jar"/>
          </fileset>
          <pathelement path ="${classpath}"/>
          </path>
          <target name="schema" description="Generate DB schema from the O/R mapping files">
          <!-- Teach Ant how to use Hibernate's schema generation tool -->
          <taskdef name="schemaexport"
          classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"
          classpathref="project.class.path"/>
          <schemaexport properties="${source.root}/hibernate.properties"
          quiet="no" text="no" drop="no" delimiter=";">
          <fileset dir="${source.root}">
          <include name="**/*.hbm.xml"/>
          </fileset>
          </schemaexport>
          </target>
          </project>

           <taskdef/>標籤定義一個新的任務schemaexport,相關的屬性設定是根據參考手冊的建議設定的,我們在這邊使用 hibernate.properties來告訴SchemaExportTask相關的JDBC資訊,quiet、text等屬性的定義,可以看參考手冊的表15.2。

           這個Ant建構檔案,會找尋src目錄下包括子目錄中有的*.hbm.xml,並自動根據映射資訊建立表格,我們還必須提供hibernate.properties(置於src下)來告知JDBC連接的相關訊息:

          hibernate.properties
          hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect
          hibernate.connection.driver_class=com.mysql.jdbc.Driver
          hibernate.connection.url=jdbc:mysql://localhost/HibernateTest
          hibernate.connection.username=caterpillar
          hibernate.connection.password=123456

           這邊使用的是MySQL,請實際根據您所使用的資料庫設定dialect、驅動程式等資訊,在開始運行Ant使用SchemaExportTask進行自動表格建立之前,您要先建立資料庫,這邊的例子則是在MySQL中先建立HibernateTest:

          mysql> create database HibernateTest;
          Query OK, 1 row affected (0.03 sec)

           接著就可以運行Ant了,執行結果如下:

          ant
          Buildfile: build.xml
          schema:
          [schemaexport] log4j:WARN No appenders could be found for logger (net.sf.hiberna
          te.cfg.Environment).
          [schemaexport] log4j:WARN Please initialize the log4j system properly.
          [schemaexport] drop table if exists USER;
          [schemaexport] create table USER (
          [schemaexport]    user_id char(32) not null,
          [schemaexport]    name varchar(16) not null,
          [schemaexport]    sex char(1),
          [schemaexport]    age integer,
          [schemaexport]    primary key (user_id)
          [schemaexport] );
          BUILD SUCCESSFUL
          Total time: 5 seconds

           運行的過程中,我們可以看到建立表格的SQL語句,而自動建立好的資料庫表格資訊如下:

          mysql> DESCRIBE user;
          +---------+-------------+------+-----+---------+-------+
          | Field   | Type        | Null | Key | Default | Extra |
          +---------+-------------+------+-----+---------+-------+
          | user_id | varchar(32) |      | PRI |         |       |
          | name    | varchar(16) |      |     |         |       |
          | sex     | char(1)     | YES  |     | NULL    |       |
          | age     | int(11)     | YES  |     | NULL    |       |
          +---------+-------------+------+-----+---------+-------+
          4 rows in set (0.04 sec)

          更多有關SchemaExportTask的資訊,可以看看參考手冊的第15章工具箱指南的部份。

          posted on 2009-04-11 11:37 lanxin1020 閱讀(263) 評論(0)  編輯  收藏 所屬分類: hibernate
          主站蜘蛛池模板: 岳池县| 阿拉善右旗| 子洲县| 阿坝| 汕尾市| 石渠县| 江达县| 彝良县| 信宜市| 驻马店市| 齐河县| 吕梁市| 安宁市| 南汇区| 区。| 无锡市| 疏勒县| 通州市| 特克斯县| 双流县| 洛川县| 屏东县| 霍山县| 社旗县| 昂仁县| 竹北市| 清水河县| 凤凰县| 徐闻县| 鲁甸县| 平安县| 聂拉木县| 宜兰市| 噶尔县| 巴东县| 南安市| 安国市| 麻栗坡县| 濮阳市| 全南县| 彭泽县|