407 lines
17 KiB
XML
407 lines
17 KiB
XML
<chapter id="toolsetguide" revision="2">
|
||
<title>工具箱指南</title>
|
||
|
||
<para>
|
||
可以通过一系列Eclipse插件、命令行工具和Ant任务来进行与Hibernate关联的转换。
|
||
|
||
</para>
|
||
|
||
<para>
|
||
除了Ant任务外,当前的<emphasis>Hibernate Tools</emphasis>也包含了Eclipse IDE的插件,用于与现存数据库的逆向工程。
|
||
</para>
|
||
|
||
<itemizedlist>
|
||
<listitem><para>
|
||
|
||
<emphasis>Mapping Editor:</emphasis> Hibernate XML映射文件的编辑器,支持自动完成和语法高亮。它也支持对类名和属性/字段名的语义自动完成,比通常的XML编辑器方便得多。
|
||
</para></listitem>
|
||
<listitem><para>
|
||
<emphasis>Console:</emphasis> Console是Eclipse的一个新视图。除了对你的console配置的树状概览,你还可以获得对你持久化类及其关联的交互式视图。Console允许你对数据库执行HQL查询,并直接在Eclipse中浏览结果。
|
||
</para></listitem>
|
||
<listitem><para>
|
||
<emphasis>Development Wizards:</emphasis> 在Hibernate Eclipse tools中还提供了几个向导;你可以用向导快速生成Hibernate 配置文件(cfg.xml),你甚至还可以同现存的数据库schema中反向工程出POJO源代码与Hibernate 映射文件。反向工程支持可定制的模版。
|
||
</para></listitem>
|
||
<listitem><para>
|
||
<emphasis>Ant Tasks:</emphasis>
|
||
</para></listitem>
|
||
|
||
</itemizedlist>
|
||
|
||
<para>
|
||
要得到更多信息,请查阅 <emphasis>Hibernate Tools</emphasis> 包及其文档。
|
||
</para>
|
||
|
||
<para>
|
||
同时,Hibernate主发行包还附带了一个集成的工具(它甚至可以在Hibernate“内部”快速运行)<emphasis>SchemaExport</emphasis> ,也就是 <literal>hbm2ddl</literal>。
|
||
|
||
</para>
|
||
|
||
<sect1 id="toolsetguide-s1" revision="2">
|
||
<title>Schema自动生成(Automatic schema generation)</title>
|
||
|
||
<para>
|
||
可以从你的映射文件使用一个Hibernate工具生成DDL。 生成的schema包含有对实体和集合类表的完整性引用约束(主键和外键)。涉及到的标示符生成器所需的表和sequence也会同时生成。
|
||
</para>
|
||
|
||
<para>
|
||
在使用这个工具的时候,你<emphasis>必须</emphasis> 通过<literal>hibernate.dialet</literal>属性指定一个SQL<literal>方言(Dialet)</literal>,因为DDL是与供应商高度相关的。
|
||
</para>
|
||
|
||
<para>
|
||
首先,要定制你的映射文件,来改善生成的schema。
|
||
</para>
|
||
|
||
<sect2 id="toolsetguide-s1-2" revision="1">
|
||
<title>对schema定制化(Customizing the schema)</title>
|
||
|
||
<para>
|
||
很多Hibernate映射元素定义了一个可选的<literal>length</literal>属性。你可以通过这个属性设置字段的长度。 (如果是Or, for numeric/decimal data types, the precision.)
|
||
</para>
|
||
|
||
<para>
|
||
有些tag接受<literal>not-null</literal>属性(用来在表字段上生成<literal>NOT NULL</literal>约束)和<literal>unique</literal>属性(用来在表字段上生成<literal>UNIQUE</literal>约束)。
|
||
</para>
|
||
|
||
<para>
|
||
有些tag接受<literal>index</literal>属性,用来指定字段的index名字。<literal>unique-key</literal>属性可以对成组的字段指定一个组合键约束(unit key constraint)。目前,<literal>unique-key</literal>属性指定的值<emphasis>并不会</emphasis>被当作这个约束的名字,它们只是在用来在映射文件内部用作区分的。
|
||
</para>
|
||
|
||
<para>
|
||
示例:
|
||
</para>
|
||
|
||
<programlisting><![CDATA[<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"/>]]></programlisting>
|
||
|
||
<para>
|
||
另外,这些元素还接受<literal><column></literal>子元素。在定义跨越多字段的类型时特别有用。
|
||
</para>
|
||
|
||
<programlisting><![CDATA[<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>]]></programlisting>
|
||
|
||
<para>
|
||
<literal>sql-type</literal>属性允许用户覆盖默认的Hibernate类型到SQL数据类型的映射。
|
||
</para>
|
||
|
||
<para>
|
||
<literal>check</literal>属性允许用户指定一个约束检查。
|
||
</para>
|
||
|
||
<programlisting><![CDATA[<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>]]></programlisting>
|
||
|
||
|
||
<table frame="topbot" id="schemattributes-summary" revision="1">
|
||
<title>Summary</title>
|
||
<tgroup cols="2">
|
||
<colspec colwidth="1*"/>
|
||
<colspec colwidth="2.5*"/>
|
||
<thead>
|
||
<row>
|
||
<entry>属性(Attribute)</entry>
|
||
<entry>值(Values)</entry>
|
||
<entry>解释(Interpretation)</entry>
|
||
</row>
|
||
</thead>
|
||
<tbody>
|
||
<row>
|
||
<entry><literal>length</literal></entry>
|
||
<entry>数字</entry>
|
||
<entry>字段长度/小数点精度</entry>
|
||
|
||
</row>
|
||
<row>
|
||
<entry><literal>not-null</literal></entry>
|
||
<entry><literal>true|false</literal></entry>
|
||
<entry>指明字段是否应该是非空的</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>unique</literal></entry>
|
||
<entry><literal>true|false</literal></entry>
|
||
<entry>指明是否该字段具有惟一约束</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>index</literal></entry>
|
||
<entry><literal>index_name</literal></entry>
|
||
<entry>指明一个(多字段)的索引(index)的名字</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>unique-key</literal></entry>
|
||
<entry><literal>unique_key_name</literal></entry>
|
||
<entry>指明多字段惟一约束的名字(参见上面的说明)</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>foreign-key</literal></entry>
|
||
<entry><literal>foreign_key_name</literal></entry>
|
||
<entry>
|
||
指明一个外键的名字,它是为关联生成的。
|
||
</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>sql-type</literal></entry>
|
||
<entry><literal>column_type</literal></entry>
|
||
<entry>
|
||
覆盖默认的字段类型(只能用于<literal><column></literal>属性)
|
||
</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>check</literal></entry>
|
||
<entry>SQL 表达式</entry>
|
||
<entry>
|
||
对字段或表加入SQL约束检查
|
||
</entry>
|
||
</row>
|
||
|
||
</tbody>
|
||
</tgroup>
|
||
</table>
|
||
|
||
</sect2>
|
||
|
||
<sect2 id="toolsetguide-s1-3">
|
||
<title>运行该工具</title>
|
||
|
||
<para>
|
||
<literal>SchemaExport</literal>工具把DDL脚本写到标准输出,同时/或者执行DDL语句。
|
||
</para>
|
||
|
||
<para>
|
||
<literal>java -cp </literal><emphasis>hibernate_classpaths</emphasis>
|
||
<literal>org.hibernate.tool.hbm2ddl.SchemaExport</literal> <emphasis>options mapping_files</emphasis>
|
||
</para>
|
||
|
||
<table frame="topbot">
|
||
<title><literal>SchemaExport</literal>命令行选项</title>
|
||
<tgroup cols="2">
|
||
<colspec colwidth="1.5*"/>
|
||
<colspec colwidth="2*"/>
|
||
<thead>
|
||
<row>
|
||
<entry>选项</entry>
|
||
<entry>说明</entry>
|
||
</row>
|
||
</thead>
|
||
<tbody>
|
||
<row>
|
||
<entry><literal>--quiet</literal></entry>
|
||
<entry>不要把脚本输出到stdout</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>--drop</literal></entry>
|
||
<entry>只进行drop tables的步骤</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>--text</literal></entry>
|
||
<entry>不执行在数据库中运行的步骤</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>--output=my_schema.ddl</literal></entry>
|
||
<entry>把输出的ddl脚本输出到一个文件</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>--config=hibernate.cfg.xml</literal></entry>
|
||
<entry>从XML文件读入Hibernate配置</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>--properties=hibernate.properties</literal></entry>
|
||
<entry>从文件读入数据库属性</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>--format</literal></entry>
|
||
<entry>把脚本中的SQL语句对齐和美化</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>--delimiter=x</literal></entry>
|
||
<entry>为脚本设置行结束符</entry>
|
||
</row>
|
||
</tbody>
|
||
</tgroup>
|
||
</table>
|
||
|
||
<para>
|
||
你甚至可以在你的应用程序中嵌入<literal>SchemaExport</literal>工具:
|
||
</para>
|
||
|
||
<programlisting><![CDATA[Configuration cfg = ....;
|
||
new SchemaExport(cfg).create(false, true);]]></programlisting>
|
||
|
||
</sect2>
|
||
|
||
<sect2 id="toolsetguide-s1-4">
|
||
<title>属性(Properties)</title>
|
||
|
||
<para>
|
||
可以通过如下方式指定数据库属性:
|
||
</para>
|
||
|
||
<itemizedlist spacing="compact">
|
||
<listitem>
|
||
<para>通过<literal>-D</literal><emphasis><property></emphasis>系统参数</para>
|
||
</listitem>
|
||
<listitem>
|
||
<para>在<literal>hibernate.properties</literal>文件中</para>
|
||
</listitem>
|
||
<listitem>
|
||
<para>位于一个其它名字的properties文件中,然后用 <literal>--properties</literal>参数指定</para>
|
||
</listitem>
|
||
</itemizedlist>
|
||
|
||
<para>
|
||
所需的参数包括:
|
||
</para>
|
||
|
||
<table frame="topbot">
|
||
<title>SchemaExport 连接属性</title>
|
||
<tgroup cols="2">
|
||
<colspec colwidth="1.5*"/>
|
||
<colspec colwidth="2*"/>
|
||
<thead>
|
||
<row>
|
||
<entry>属性名</entry>
|
||
<entry>说明</entry>
|
||
</row>
|
||
</thead>
|
||
<tbody>
|
||
<row>
|
||
<entry><literal>hibernate.connection.driver_class</literal></entry>
|
||
<entry>jdbc driver class</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>hibernate.connection.url</literal></entry>
|
||
<entry>jdbc url</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>hibernate.connection.username</literal></entry>
|
||
<entry>database user</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>hibernate.connection.password</literal></entry>
|
||
<entry>user password</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>hibernate.dialect</literal></entry>
|
||
<entry>方言(dialect)</entry>
|
||
</row>
|
||
</tbody>
|
||
</tgroup>
|
||
</table>
|
||
|
||
</sect2>
|
||
|
||
<sect2 id="toolsetguide-s1-5">
|
||
<title>使用Ant(Using Ant)</title>
|
||
|
||
<para>
|
||
你可以在你的Ant build脚本中调用<literal>SchemaExport</literal>:
|
||
</para>
|
||
|
||
<programlisting><![CDATA[<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>]]></programlisting>
|
||
|
||
</sect2>
|
||
|
||
<sect2 id="toolsetguide-s1-6">
|
||
<title>对schema的增量更新(Incremental schema updates)</title>
|
||
|
||
<para>
|
||
<literal>SchemaUpdate</literal>工具对已存在的schema采用"增量"方式进行更新。注意<literal>SchemaUpdate</literal>严重依赖于JDBC metadata API,所以它并非对所有JDBC驱动都有效。
|
||
</para>
|
||
|
||
<para>
|
||
<literal>java -cp </literal><emphasis>hibernate_classpaths</emphasis>
|
||
<literal>org.hibernate.tool.hbm2ddl.SchemaUpdate</literal> <emphasis>options mapping_files</emphasis>
|
||
</para>
|
||
|
||
<table frame="topbot">
|
||
<title><literal>SchemaUpdate</literal>命令行选项</title>
|
||
<tgroup cols="2">
|
||
<colspec colwidth="1.5*"/>
|
||
<colspec colwidth="2*"/>
|
||
<thead>
|
||
<row>
|
||
<entry>选项</entry>
|
||
<entry>说明</entry>
|
||
</row>
|
||
</thead>
|
||
<tbody>
|
||
<row>
|
||
<entry><literal>--quiet</literal></entry>
|
||
<entry>不要把脚本输出到stdout</entry>
|
||
</row>
|
||
<row>
|
||
<entry><literal>--properties=hibernate.properties</literal></entry>
|
||
<entry>从指定文件读入数据库属性</entry>
|
||
</row>
|
||
</tbody>
|
||
</tgroup>
|
||
</table>
|
||
|
||
<para>
|
||
你可以在你的应用程序中嵌入<literal>SchemaUpdate</literal>工具:
|
||
</para>
|
||
|
||
<programlisting><![CDATA[Configuration cfg = ....;
|
||
new SchemaUpdate(cfg).execute(false);]]></programlisting>
|
||
|
||
</sect2>
|
||
|
||
<sect2 id="toolsetguide-s1-7">
|
||
<title>用Ant来增量更新schema(Using Ant for incremental schema updates)</title>
|
||
|
||
<para>
|
||
你可以在Ant脚本中调用<literal>SchemaUpdate</literal>:
|
||
</para>
|
||
|
||
<programlisting><![CDATA[<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>]]></programlisting>
|
||
|
||
</sect2>
|
||
|
||
</sect1>
|
||
|
||
</chapter>
|
||
|