🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 20.1.1\. 对schema定制化(Customizing the schema) 很多Hibernate映射元素定义了可选的`length`、`precision` 或者 `scale`属性。你可以通过这个属性设置字段的长度、精度、小数点位数。 ``` <property name="zip" length="5"/> ``` ``` <property name="balance" precision="12" scale="2"/> ``` 有些tag还接受`not-null`属性(用来在表字段上生成`NOT NULL`约束)和`unique`属性(用来在表字段上生成`UNIQUE`约束)。 ``` <many-to-one name="bar" column="barId" not-null="true"/> ``` ``` <element column="serialNumber" type="long" not-null="true" unique="true"/> ``` `unique-key`属性可以对成组的字段指定一个唯一键约束(unique key constraint)。目前,`unique-key`属性指定的值在生成DDL时_并不会_被当作这个约束的名字,它们只是在用来在映射文件内部用作区分的。 ``` <many-to-one name="org" column="orgId" unique-key="OrgEmployeeId"/> <property name="employeeId" unique-key="OrgEmployee"/> ``` `index`属性会用对应的字段(一个或多个)生成一个index,它指出了这个index的名字。如果多个字段对应的index名字相同,就会生成包含这些字段的index。 ``` <property name="lastName" index="CustName"/> <property name="firstName" index="CustName"/> ``` `foreign-key`属性可以用来覆盖任何生成的外键约束的名字。 ``` <many-to-one name="bar" column="barId" foreign-key="FKFooBar"/> ``` 很多映射元素还接受`&lt;column&gt;`子元素。这在定义跨越多字段的类型时特别有用。 ``` <property name="name" type="my.customtypes.Name"/> <column name="last" not-null="true" index="bar_idx" length="30"/> <column name="first" not-null="true" index="bar_idx" length="20"/> <column name="initial"/> </property> ``` `default`属性为字段指定一个默认值 (在保存被映射的类的新实例之前,你应该将同样的值赋于对应的属性)。 ``` <property name="credits" type="integer" insert="false"> <column name="credits" default="10"/> </property> ``` ``` <version name="version" type="integer" insert="false"> <column name="version" default="0"/> </property> ``` `sql-type`属性允许用户覆盖默认的Hibernate类型到SQL数据类型的映射。 ``` <property name="balance" type="float"> <column name="balance" sql-type="decimal(13,3)"/> </property> ``` `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> ``` **表 20.1\. Summary** | 属性(Attribute) | 值(Values) | 解释(Interpretation) | | --- | --- | --- | | `length` | 数字 | 字段长度 | | `precision` | 数字 | 精度(decimal precision) | | `scale` | 数字 | 小数点位数(decimal scale) | | `not-null` | `true&#124;false` | 指明字段是否应该是非空的 | | `unique` | `true&#124;false` | 指明是否该字段具有惟一约束 | | `index` | `index_name` | 指明一个(多字段)的索引(index)的名字 | | `unique-key` | `unique_key_name` | 指明多字段惟一约束的名字(参见上面的说明) | | `foreign-key` | `foreign_key_name` | specifies the name of the foreign key constraint generated for an association, for a `&lt;one-to-one&gt;`, `&lt;many-to-one&gt;`, `&lt;key&gt;`, or `&lt;many-to-many&gt;` mapping element. Note that `inverse="true"` sides will not be considered by `SchemaExport`. 指明一个外键的名字,它是为关联生成的,或者`&lt;one-to-one&gt;`,`&lt;many-to-one&gt;`, `&lt;key&gt;`, 或者`&lt;many-to-many&gt;`映射元素。注意`inverse="true"`在`SchemaExport`时会被忽略。 | | `sql-type` | `SQL 字段类型` | 覆盖默认的字段类型(只能用于`&lt;column&gt;`属性) | | `default` | SQL表达式 | 为字段指定默认值 | | `check` | SQL 表达式 | 对字段或表加入SQL约束检查 | `&lt;comment&gt;`元素可以让你在生成的schema中加入注释。 ``` <class name="Customer" table="CurCust"> <comment>Current customers only</comment> ... </class> ``` ``` <property name="balance"> <column name="bal"> <comment>Balance in USD</comment> </column> </property> ``` 结果是在生成的DDL中包含`comment on table` 或者 `comment on column`语句(假若支持的话)。