在您撰寫好*.hbm.xml映射文件之後,您可以使用 net.sf.hibernate.tool.hbm2ddl.SchemaExportTask來自動建立資料庫表格,這邊所使用的方式是結(jié)合Ant進(jìn)行自動化建構(gòu),首先我們假設(shè)將使用以下的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/>標(biāo)籤用於指定建立表格時的一些資訊,例如映射的表格欄位名稱,或是sql-type或 length等屬性,如果不指定這些資訊時,SchemaExportTask將自動使用Hibernate的類型至SQL類型等資訊來建立表格;sql -type用於指定表格欄位型態(tài),not-null表示欄位不能為null,length則用於指定表格文字欄位長度,這些屬性的說明,都可以在 Hibernate參考手冊的表15.1找到。
下面的build.xml用於Ant自動化建構(gòu)時,生成資料庫表格之用:
<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/>標(biāo)籤定義一個新的任務(wù)schemaexport,相關(guān)的屬性設(shè)定是根據(jù)參考手冊的建議設(shè)定的,我們在這邊使用 hibernate.properties來告訴SchemaExportTask相關(guān)的JDBC資訊,quiet、text等屬性的定義,可以看參考手冊的表15.2。
這個Ant建構(gòu)檔案,會找尋src目錄下包括子目錄中有的*.hbm.xml,並自動根據(jù)映射資訊建立表格,我們還必須提供hibernate.properties(置於src下)來告知JDBC連接的相關(guān)訊息:
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,請實(shí)際根據(jù)您所使用的資料庫設(shè)定dialect、驅(qū)動程式等資訊,在開始運(yùn)行Ant使用SchemaExportTask進(jìn)行自動表格建立之前,您要先建立資料庫,這邊的例子則是在MySQL中先建立HibernateTest:
mysql> create database HibernateTest; Query OK, 1 row affected (0.03 sec)
接著就可以運(yùn)行Ant了,執(zhí)行結(jié)果如下:
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
運(yùn)行的過程中,我們可以看到建立表格的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)
更多有關(guān)SchemaExportTask的資訊,可以看看參考手冊的第15章工具箱指南的部份。