21.1. Schema自动生成(Automatic schema generation)
 
 
 
可以从你的映射文件使用一个Hibernate工具生成DDL。 生成的schema包含有对实体和集合类表的完整性引用约束(主键和外键)。涉及到的标示符生成器所需的表和sequence也会同时生成。 
在使用这个工具的时候,你必须 通过hibernate.dialet属性指定一个SQL方言(Dialet),因为DDL是与供应商高度相关的。 
首先,要定制你的映射文件,来改善生成的schema。 
21.1.1. 对schema定制化(Customizing the schema)
 
 
 
很多Hibernate映射元素定义了一个可选的length属性。你可以通过这个属性设置字段的长度。 (如果是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属性指定的值并不会被当作这个约束的名字,它们只是在用来在映射文件内部用作区分的。 
示例: 
<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数据类型的映射。 
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 | 数字 | 字段长度/小数点精度 | 
        
            | not-null | true|false | 指明字段是否应该是非空的 | 
        
            | unique | true|false | 指明是否该字段具有惟一约束 | 
        
            | index | index_name | 指明一个(多字段)的索引(index)的名字 | 
        
            | unique-key | unique_key_name | 指明多字段惟一约束的名字(参见上面的说明) | 
        
            | foreign-key | foreign_key_name | 指明一个外键的名字,它是为关联生成的。 | 
        
            | sql-type | column_type | 覆盖默认的字段类型(只能用于<column>属性) | 
        
            | check | SQL 表达式 | 对字段或表加入SQL约束检查 | 
    
 
 
SchemaExport工具把DDL脚本写到标准输出,同时/或者执行DDL语句。 
java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaExport options mapping_files 
表 21.2. SchemaExport命令行选项
    
    
    
    
        
            | 选项 | 说明 | 
    
    
        
            | --quiet | 不要把脚本输出到stdout | 
        
            | --drop | 只进行drop tables的步骤 | 
        
            | --text | 不执行在数据库中运行的步骤 | 
        
            | --output=my_schema.ddl | 把输出的ddl脚本输出到一个文件 | 
        
            | --config=hibernate.cfg.xml | 从XML文件读入Hibernate配置 | 
        
            | --properties=hibernate.properties | 从文件读入数据库属性 | 
        
            | --format | 把脚本中的SQL语句对齐和美化 | 
        
            | --delimiter=x | 为脚本设置行结束符 | 
    
 
你甚至可以在你的应用程序中嵌入SchemaExport工具: 
Configuration cfg = ....;
new SchemaExport(cfg).create(false, true);
 
可以通过如下方式指定数据库属性: 
所需的参数包括: 
表 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) | 
    
 
 
你可以在你的Ant build脚本中调用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驱动都有效。 
java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaUpdate options mapping_files 
表 21.4. SchemaUpdate命令行选项
    
    
    
    
        
            | 选项 | 说明 | 
    
    
        
            | --quiet | 不要把脚本输出到stdout | 
        
            | --properties=hibernate.properties | 从指定文件读入数据库属性 | 
    
 
你可以在你的应用程序中嵌入SchemaUpdate工具: 
Configuration cfg = ....;
new SchemaUpdate(cfg).execute(false);
 
21.1.6. 用Ant来增量更新schema(Using Ant for incremental schema updates)
 
 
 
你可以在Ant脚本中调用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>