~raginggoblin/infolog/infolog

« back to all changes in this revision

Viewing changes to InfologServer/lib/hibernate-distribution-3.3.2.GA/documentation/manual/pt-BR/html/toolsetguide.html

  • Committer: Raging Goblin
  • Date: 2013-11-16 16:51:32 UTC
  • Revision ID: raging_goblin-20131116165132-weujnptzc88uy4ah
Mavenized the project, now using shared project InfologSync

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2
 
<!DOCTYPE html
3
 
  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4
 
<html xmlns="http://www.w3.org/1999/xhtml"><head><title xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory">Capítulo 20. Toolset Guide</title><link rel="stylesheet" href="css/hibernate.css" type="text/css"/><meta xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" name="generator" content="DocBook XSL Stylesheets V1.72.0"/><link rel="start" href="index.html" title="HIBERNATE - Relational Persistence for Idiomatic Java"/><link rel="up" href="index.html" title="HIBERNATE - Relational Persistence for Idiomatic Java"/><link rel="prev" href="performance.html" title="Capítulo 19. Aumentando a performance"/><link rel="next" href="example-parentchild.html" title="Capítulo 21. Example: Parent/Child"/><link rel="copyright" href="ln-Legal_Notice.html" title="Legal Notice"/></head><body><p id="title"><a href="http://www.hibernate.org" class="site_href"><strong>Hibernate.org</strong></a><a href="http://hibernate.org/Documentation/DocumentationOverview" class="doc_href"><strong>Community Documentation</strong></a></p><ul class="docnav"><li class="previous"><a accesskey="p" href="performance.html"><strong>Anterior</strong></a></li><li class="next"><a accesskey="n" href="example-parentchild.html"><strong>Próxima</strong></a></li></ul><div class="chapter" lang="pt-BR"><div class="titlepage"><div><div><h2 class="title"><a id="toolsetguide"/>Capítulo 20. Toolset Guide</h2></div></div></div><div class="toc"><dl><dt><span class="sect1"><a href="toolsetguide.html#toolsetguide-s1">20.1. Automatic schema generation</a></span></dt><dd><dl><dt><span class="sect2"><a href="toolsetguide.html#toolsetguide-s1-2">20.1.1. Customizing the schema</a></span></dt><dt><span class="sect2"><a href="toolsetguide.html#toolsetguide-s1-3">20.1.2. Running the tool</a></span></dt><dt><span class="sect2"><a href="toolsetguide.html#toolsetguide-s1-4">20.1.3. Properties</a></span></dt><dt><span class="sect2"><a href="toolsetguide.html#toolsetguide-s1-5">20.1.4. Using Ant</a></span></dt><dt><span class="sect2"><a href="toolsetguide.html#toolsetguide-s1-6">20.1.5. Incremental schema updates</a></span></dt><dt><span class="sect2"><a href="toolsetguide.html#toolsetguide-s1-7">20.1.6. Using Ant for incremental schema updates</a></span></dt><dt><span class="sect2"><a href="toolsetguide.html#toolsetguide-s1-8">20.1.7. Schema validation</a></span></dt><dt><span class="sect2"><a href="toolsetguide.html#toolsetguide-s1-9">20.1.8. Using Ant for schema validation</a></span></dt></dl></dd></dl></div><p>Roundtrip engineering with Hibernate is possible using a set of Eclipse plugins, commandline tools, and Ant tasks. </p><p><span class="emphasis"><em>Hibernate Tools</em></span> currently include plugins for the Eclipse IDE as well as Ant tasks for reverse engineering of existing databases: </p><div class="itemizedlist"><ul><li><p><span class="emphasis"><em>Mapping Editor:</em></span> an editor for Hibernate XML mapping files that supports auto-completion and syntax highlighting. It also supports semantic auto-completion for class names and property/field names, making it more versatile than a normal XML editor. </p></li><li><p><span class="emphasis"><em>Console:</em></span> the console is a new view in Eclipse. In addition to a tree overview of your console configurations, you are also provided with an interactive view of your persistent classes and their relationships. The console allows you to execute HQL queries against your database and browse the result directly in Eclipse. </p></li><li><p><span class="emphasis"><em>Development Wizards:</em></span> several wizards are provided with the Hibernate Eclipse tools. You can use a wizard to quickly generate Hibernate configuration (cfg.xml) files, or to reverse engineer an existing database schema into POJO source files and Hibernate mapping files. The reverse engineering wizard supports customizable templates. </p></li><li><p>
5
 
 
6
 
        </p></li></ul></div><p>Please refer to the <span class="emphasis"><em>Hibernate Tools</em></span> package documentation for more information. </p><p>However, the Hibernate main package comes bundled with an integrated tool : <span class="emphasis"><em>SchemaExport</em></span> aka <code class="literal">hbm2ddl</code>.It can even be used from "inside" Hibernate. </p><div class="sect1" lang="pt-BR"><div class="titlepage"><div><div><h2 class="title"><a id="toolsetguide-s1"/>20.1. Automatic schema generation</h2></div></div></div><p>DDL can be generated from your mapping files by a Hibernate utility. The generated schema includes referential integrity constraints, primary and foreign keys, for entity and collection tables. Tables and sequences are also created for mapped identifier generators. </p><p>You <span class="emphasis"><em>must</em></span> specify a SQL <code class="literal">Dialect</code> via the <code class="literal">hibernate.dialect</code> property when using this tool, as DDL is highly vendor-specific. </p><p>First, you must customize your mapping files to improve the generated schema. The next section covers schema customization. </p><div class="sect2" lang="pt-BR"><div class="titlepage"><div><div><h3 class="title"><a id="toolsetguide-s1-2"/>20.1.1. Customizing the schema</h3></div></div></div><p>Many Hibernate mapping elements define optional attributes named <code class="literal">length</code>, <code class="literal">precision</code> and <code class="literal">scale</code>. You can set the length, precision and scale of a column with this attribute. </p><pre class="programlisting">&lt;property name="zip" length="5"/&gt;</pre><pre class="programlisting">&lt;property name="balance" precision="12" scale="2"/&gt;</pre><p>Some tags also accept a <code class="literal">not-null</code> attribute for generating a <code class="literal">NOT NULL</code> constraint on table columns, and a <code class="literal">unique</code> attribute for generating <code class="literal">UNIQUE</code> constraint on table columns. </p><pre class="programlisting">&lt;many-to-one name="bar" column="barId" not-null="true"/&gt;</pre><pre class="programlisting">&lt;element column="serialNumber" type="long" not-null="true" unique="true"/&gt;</pre><p>A <code class="literal">unique-key</code> attribute can be used to group columns in a single, unique key constraint. Currently, the specified value of the <code class="literal">unique-key</code> attribute is <span class="emphasis"><em>not</em></span> used to name the constraint in the generated DDL. It is only used to group the columns in the mapping file. </p><pre class="programlisting">&lt;many-to-one name="org" column="orgId" unique-key="OrgEmployeeId"/&gt;
7
 
&lt;property name="employeeId" unique-key="OrgEmployee"/&gt;</pre><p>An <code class="literal">index</code> attribute specifies the name of an index that will be created using the mapped column or columns. Multiple columns can be grouped into the same index by simply specifying the same index name. </p><pre class="programlisting">&lt;property name="lastName" index="CustName"/&gt;
8
 
&lt;property name="firstName" index="CustName"/&gt;</pre><p>A <code class="literal">foreign-key</code> attribute can be used to override the name of any generated foreign key constraint. </p><pre class="programlisting">&lt;many-to-one name="bar" column="barId" foreign-key="FKFooBar"/&gt;</pre><p>Many mapping elements also accept a child <code class="literal">&lt;column&gt;</code> element. This is particularly useful for mapping multi-column types: </p><pre class="programlisting">&lt;property name="name" type="my.customtypes.Name"/&gt;
9
 
    &lt;column name="last" not-null="true" index="bar_idx" length="30"/&gt;
10
 
    &lt;column name="first" not-null="true" index="bar_idx" length="20"/&gt;
11
 
    &lt;column name="initial"/&gt;
12
 
&lt;/property&gt;</pre><p>The <code class="literal">default</code> attribute allows you to specify a default value for a column.You should assign the same value to the mapped property before saving a new instance of the mapped class. </p><pre class="programlisting">&lt;property name="credits" type="integer" insert="false"&gt;
13
 
    &lt;column name="credits" default="10"/&gt;
14
 
&lt;/property&gt;</pre><pre class="programlisting">&lt;version name="version" type="integer" insert="false"&gt;
15
 
    &lt;column name="version" default="0"/&gt;
16
 
&lt;/property&gt;</pre><p>The <code class="literal">sql-type</code> attribute allows the user to override the default mapping of a Hibernate type to SQL datatype. </p><pre class="programlisting">&lt;property name="balance" type="float"&gt;
17
 
    &lt;column name="balance" sql-type="decimal(13,3)"/&gt;
18
 
&lt;/property&gt;</pre><p>The <code class="literal">check</code> attribute allows you to specify a check constraint. </p><pre class="programlisting">&lt;property name="foo" type="integer"&gt;
19
 
    &lt;column name="foo" check="foo &gt; 10"/&gt;
20
 
&lt;/property&gt;</pre><pre class="programlisting">&lt;class name="Foo" table="foos" check="bar &lt; 100.0"&gt;
21
 
    ...
22
 
    &lt;property name="bar" type="float"/&gt;
23
 
&lt;/class&gt;</pre><p>The following table summarizes these optional attributes.</p><div class="table"><a id="schemattributes-summary"/><p class="title"><b>Tabela 20.1. Summary</b></p><div class="table-contents"><table summary="Summary" border="1"><colgroup><col/><col/><col/></colgroup><thead><tr><th>Attribute</th><th>Values</th><th>Interpretation</th></tr></thead><tbody><tr><td><code class="literal">length</code></td><td>number</td><td>column length</td></tr><tr><td><code class="literal">precision</code></td><td>number</td><td>column decimal precision</td></tr><tr><td><code class="literal">scale</code></td><td>number</td><td>column decimal scale</td></tr><tr><td><code class="literal">not-null</code></td><td><code class="literal">true|false</code></td><td>specifies that the column should be non-nullable</td></tr><tr><td><code class="literal">unique</code></td><td><code class="literal">true|false</code></td><td>specifies that the column should have a unique constraint</td></tr><tr><td><code class="literal">index</code></td><td><code class="literal">index_name</code></td><td>specifies the name of a (multi-column) index</td></tr><tr><td><code class="literal">unique-key</code></td><td><code class="literal">unique_key_name</code></td><td>specifies the name of a multi-column unique constraint</td></tr><tr><td><code class="literal">foreign-key</code></td><td><code class="literal">foreign_key_name</code></td><td>specifies the name of the foreign key constraint generated for an association, for a <code class="literal">&lt;one-to-one&gt;</code>, <code class="literal">&lt;many-to-one&gt;</code>, <code class="literal">&lt;key&gt;</code>, or <code class="literal">&lt;many-to-many&gt;</code> mapping element. Note that <code class="literal">inverse="true"</code> sides will not be considered by <code class="literal">SchemaExport</code>. </td></tr><tr><td><code class="literal">sql-type</code></td><td><code class="literal">SQL column type</code></td><td>overrides the default column type (attribute of <code class="literal">&lt;column&gt;</code> element only) </td></tr><tr><td><code class="literal">default</code></td><td>SQL expression</td><td>specify a default value for the column </td></tr><tr><td><code class="literal">check</code></td><td>SQL expression</td><td>create an SQL check constraint on either column or table </td></tr></tbody></table></div></div><br class="table-break"/><p>The <code class="literal">&lt;comment&gt;</code> element allows you to specify comments for the generated schema. </p><pre class="programlisting">&lt;class name="Customer" table="CurCust"&gt;
24
 
    &lt;comment&gt;Current customers only&lt;/comment&gt;
25
 
    ...
26
 
&lt;/class&gt;</pre><pre class="programlisting">&lt;property name="balance"&gt;
27
 
    &lt;column name="bal"&gt;
28
 
        &lt;comment&gt;Balance in USD&lt;/comment&gt;
29
 
    &lt;/column&gt;
30
 
&lt;/property&gt;</pre><p>This results in a <code class="literal">comment on table</code> or <code class="literal">comment on column</code> statement in the generated DDL where supported. </p></div><div class="sect2" lang="pt-BR"><div class="titlepage"><div><div><h3 class="title"><a id="toolsetguide-s1-3"/>20.1.2. Running the tool</h3></div></div></div><p>The <code class="literal">SchemaExport</code> tool writes a DDL script to standard out and/or executes the DDL statements. </p><p>The following table displays the <code class="literal">SchemaExport</code> command line options</p><p><code class="literal">java -cp </code><span class="emphasis"><em>hibernate_classpaths</em></span> <code class="literal">org.hibernate.tool.hbm2ddl.SchemaExport</code> <span class="emphasis"><em>options mapping_files</em></span> </p><div class="table"><a id="d0e15829"/><p class="title"><b>Tabela 20.2. <code class="literal">SchemaExport</code> Command Line Options</b></p><div class="table-contents"><table summary="SchemaExport Command Line Options" border="1"><colgroup><col/><col/></colgroup><thead><tr><th>Option</th><th>Description</th></tr></thead><tbody><tr><td><code class="literal">--quiet</code></td><td>do not output the script to stdout</td></tr><tr><td><code class="literal">--drop</code></td><td>only drop the tables</td></tr><tr><td><code class="literal">--create</code></td><td>only create the tables</td></tr><tr><td><code class="literal">--text</code></td><td>do not export to the database</td></tr><tr><td><code class="literal">--output=my_schema.ddl</code></td><td>output the ddl script to a file</td></tr><tr><td><code class="literal">--naming=eg.MyNamingStrategy</code></td><td>select a <code class="literal">NamingStrategy</code></td></tr><tr><td><code class="literal">--config=hibernate.cfg.xml</code></td><td>read Hibernate configuration from an XML file</td></tr><tr><td><code class="literal">--properties=hibernate.properties</code></td><td>read database properties from a file</td></tr><tr><td><code class="literal">--format</code></td><td>format the generated SQL nicely in the script</td></tr><tr><td><code class="literal">--delimiter=;</code></td><td>set an end of line delimiter for the script</td></tr></tbody></table></div></div><br class="table-break"/><p>You can even embed <code class="literal">SchemaExport</code> in your application: </p><pre class="programlisting">Configuration cfg = ....;
31
 
new SchemaExport(cfg).create(false, true);</pre></div><div class="sect2" lang="pt-BR"><div class="titlepage"><div><div><h3 class="title"><a id="toolsetguide-s1-4"/>20.1.3. Properties</h3></div></div></div><p>Database properties can be specified: </p><div class="itemizedlist"><ul compact="compact"><li><p>as system properties with <code class="literal">-D</code><span class="emphasis"><em>&lt;property&gt;</em></span></p></li><li><p>in <code class="literal">hibernate.properties</code></p></li><li><p>in a named properties file with <code class="literal">--properties</code></p></li></ul></div><p>The needed properties are: </p><div class="table"><a id="d0e15938"/><p class="title"><b>Tabela 20.3. SchemaExport Connection Properties</b></p><div class="table-contents"><table summary="SchemaExport Connection Properties" border="1"><colgroup><col/><col/></colgroup><thead><tr><th>Property Name</th><th>Description</th></tr></thead><tbody><tr><td><code class="literal">hibernate.connection.driver_class</code></td><td>jdbc driver class</td></tr><tr><td><code class="literal">hibernate.connection.url</code></td><td>jdbc url</td></tr><tr><td><code class="literal">hibernate.connection.username</code></td><td>database user</td></tr><tr><td><code class="literal">hibernate.connection.password</code></td><td>user password</td></tr><tr><td><code class="literal">hibernate.dialect</code></td><td>dialect</td></tr></tbody></table></div></div><br class="table-break"/></div><div class="sect2" lang="pt-BR"><div class="titlepage"><div><div><h3 class="title"><a id="toolsetguide-s1-5"/>20.1.4. Using Ant</h3></div></div></div><p>You can call <code class="literal">SchemaExport</code> from your Ant build script: </p><pre class="programlisting">&lt;target name="schemaexport"&gt;
32
 
    &lt;taskdef name="schemaexport"
33
 
        classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
34
 
        classpathref="class.path"/&gt;
35
 
    
36
 
    &lt;schemaexport
37
 
        properties="hibernate.properties"
38
 
        quiet="no"
39
 
        text="no"
40
 
        drop="no"
41
 
        delimiter=";"
42
 
        output="schema-export.sql"&gt;
43
 
        &lt;fileset dir="src"&gt;
44
 
            &lt;include name="**/*.hbm.xml"/&gt;
45
 
        &lt;/fileset&gt;
46
 
    &lt;/schemaexport&gt;
47
 
&lt;/target&gt;</pre></div><div class="sect2" lang="pt-BR"><div class="titlepage"><div><div><h3 class="title"><a id="toolsetguide-s1-6"/>20.1.5. Incremental schema updates</h3></div></div></div><p>The <code class="literal">SchemaUpdate</code> tool will update an existing schema with "incremental" changes. The <code class="literal">SchemaUpdate</code> depends upon the JDBC metadata API and, as such, will not work with all JDBC drivers. </p><p><code class="literal">java -cp </code><span class="emphasis"><em>hibernate_classpaths</em></span> <code class="literal">org.hibernate.tool.hbm2ddl.SchemaUpdate</code> <span class="emphasis"><em>options mapping_files</em></span> </p><div class="table"><a id="d0e16014"/><p class="title"><b>Tabela 20.4. <code class="literal">SchemaUpdate</code> Command Line Options</b></p><div class="table-contents"><table summary="SchemaUpdate Command Line Options" border="1"><colgroup><col/><col/></colgroup><thead><tr><th>Option</th><th>Description</th></tr></thead><tbody><tr><td><code class="literal">--quiet</code></td><td>do not output the script to stdout</td></tr><tr><td><code class="literal">--text</code></td><td>do not export the script to the database</td></tr><tr><td><code class="literal">--naming=eg.MyNamingStrategy</code></td><td>select a <code class="literal">NamingStrategy</code></td></tr><tr><td><code class="literal">--properties=hibernate.properties</code></td><td>read database properties from a file</td></tr><tr><td><code class="literal">--config=hibernate.cfg.xml</code></td><td>specify a <code class="literal">.cfg.xml</code> file</td></tr></tbody></table></div></div><br class="table-break"/><p>You can embed <code class="literal">SchemaUpdate</code> in your application: </p><pre class="programlisting">Configuration cfg = ....;
48
 
new SchemaUpdate(cfg).execute(false);</pre></div><div class="sect2" lang="pt-BR"><div class="titlepage"><div><div><h3 class="title"><a id="toolsetguide-s1-7"/>20.1.6. Using Ant for incremental schema updates</h3></div></div></div><p>You can call <code class="literal">SchemaUpdate</code> from the Ant script: </p><pre class="programlisting">&lt;target name="schemaupdate"&gt;
49
 
    &lt;taskdef name="schemaupdate"
50
 
        classname="org.hibernate.tool.hbm2ddl.SchemaUpdateTask"
51
 
        classpathref="class.path"/&gt;
52
 
    
53
 
    &lt;schemaupdate
54
 
        properties="hibernate.properties"
55
 
        quiet="no"&gt;
56
 
        &lt;fileset dir="src"&gt;
57
 
            &lt;include name="**/*.hbm.xml"/&gt;
58
 
        &lt;/fileset&gt;
59
 
    &lt;/schemaupdate&gt;
60
 
&lt;/target&gt;</pre></div><div class="sect2" lang="pt-BR"><div class="titlepage"><div><div><h3 class="title"><a id="toolsetguide-s1-8"/>20.1.7. Schema validation</h3></div></div></div><p>The <code class="literal">SchemaValidator</code> tool will validate that the existing database schema "matches" your mapping documents. The <code class="literal">SchemaValidator</code> depends heavily upon the JDBC metadata API and, as such, will not work with all JDBC drivers. This tool is extremely useful for testing. </p><p><code class="literal">java -cp </code><span class="emphasis"><em>hibernate_classpaths</em></span> <code class="literal">org.hibernate.tool.hbm2ddl.SchemaValidator</code> <span class="emphasis"><em>options mapping_files</em></span> </p><div class="table"><a id="d0e16104"/><p class="title"><b>Tabela 20.5. <code class="literal">SchemaValidator</code> Command Line Options</b></p><div class="table-contents"><table summary="SchemaValidator Command Line Options" border="1"><colgroup><col/><col/></colgroup><thead><tr><th>Option</th><th>Description</th></tr></thead><tbody><tr><td><code class="literal">--naming=eg.MyNamingStrategy</code></td><td>select a <code class="literal">NamingStrategy</code></td></tr><tr><td><code class="literal">--properties=hibernate.properties</code></td><td>read database properties from a file</td></tr><tr><td><code class="literal">--config=hibernate.cfg.xml</code></td><td>specify a <code class="literal">.cfg.xml</code> file</td></tr></tbody></table></div></div><br class="table-break"/><p>You can embed <code class="literal">SchemaValidator</code> in your application: </p><pre class="programlisting">Configuration cfg = ....;
61
 
new SchemaValidator(cfg).validate();</pre></div><div class="sect2" lang="pt-BR"><div class="titlepage"><div><div><h3 class="title"><a id="toolsetguide-s1-9"/>20.1.8. Using Ant for schema validation</h3></div></div></div><p>You can call <code class="literal">SchemaValidator</code> from the Ant script: </p><pre class="programlisting">&lt;target name="schemavalidate"&gt;
62
 
    &lt;taskdef name="schemavalidator"
63
 
        classname="org.hibernate.tool.hbm2ddl.SchemaValidatorTask"
64
 
        classpathref="class.path"/&gt;
65
 
    
66
 
    &lt;schemavalidator
67
 
        properties="hibernate.properties"&gt;
68
 
        &lt;fileset dir="src"&gt;
69
 
            &lt;include name="**/*.hbm.xml"/&gt;
70
 
        &lt;/fileset&gt;
71
 
    &lt;/schemavalidator&gt;
72
 
&lt;/target&gt;</pre></div></div></div><HR xmlns=""/><a xmlns="" href="legalnotice.html"><p xmlns="http://www.w3.org/1999/xhtml" class="copyright">Copyright © 2004 Red Hat Middleware, LLC.</p></a><ul class="docnav"><li class="previous"><a accesskey="p" href="performance.html"><strong>Anterior</strong>Capítulo 19. Aumentando a performance</a></li><li class="up"><a accesskey="u" href="#"><strong>Acima</strong></a></li><li class="home"><a accesskey="h" href="index.html"><strong>Principal</strong></a></li><li class="next"><a accesskey="n" href="example-parentchild.html"><strong>Próxima</strong>Capítulo 21. Example: Parent/Child</a></li></ul></body></html>
 
 
b'\\ No newline at end of file'