Hibernate自动生成工具-Schema

开发 后端
Hibernate Schema自动生成可以从你的映射文件使用一个Hibernate工具生成DDL。 生成的schema包含有对实体和集合类表的完整性引用约束(主键和外键)。涉及到的标示符生成器所需的表和sequence也会同时生成。

本文我们主要介绍Hibernate Schema自动生成(Automatic schema generation)技术,希望对大家的学习带来帮助。

Hibernate Schema自动生成可以从你的映射文件使用一个Hibernate工具生成DDL。 生成的schema包含有对实体和集合类表的完整性引用约束(主键和外键)。涉及到的标示符生成器所需的表和sequence也会同时生成。

在使用这个工具的时候,你必须 通过hibernate.dialet属性指定一个SQL方言(Dialet),因为DDL是与供应商高度相关的。

首先,要定制你的映射文件,来改善生成的Hibernate schema。对Hibernate schema定制化(Customizing the schema)

很多Hibernate映射元素定义了可选的length、precision 或者 scale属性。你可以通过这个属性设置字段的长度、精度、小数点位数。

  1. <property name="zip" length="5"/> 
  2. <property name="balance" precision="12" scale="2"/> 

有些tag还接受not-null属性(用来在表字段上生成NOT NULL约束)和unique属性(用来在表字段上生成UNIQUE约束)。

  1. <many-to-one name="bar" column="barId" not-null="true"/> 
  2. <element column="serialNumber" type="long" not-null="true" unique="true"/> 

unique-key属性可以对成组的字段指定一个***键约束(unique key constraint)。目前,unique-key属性指定的值在生成DDL时并不会被当作这个约束的名字,它们只是在用来在映射文件内部用作区分的。

  1. <many-to-one name="org" column="orgId" unique-key="OrgEmployeeId"/> 
  2. <property name="employeeId" unique-key="OrgEmployee"/> 

index属性会用对应的字段(一个或多个)生成一个index,它指出了这个index的名字。如果多个字段对应的index名字相同,就会生成包含这些字段的index。

  1. <property name="lastName" index="CustName"/> 
  2. <property name="firstName" index="CustName"/> 

foreign-key属性可以用来覆盖任何生成的外键约束的名字。

  1. <many-to-one name="bar" column="barId" foreign-key="FKFooBar"/> 

很多映射元素还接受子元素。这在定义跨越多字段的类型时特别有用。

  1. <property name="name" type="my.customtypes.Name"/> 
  2.     <column name="last" not-null="true" index="bar_idx" length="30"/> 
  3.     <column name="first" not-null="true" index="bar_idx" length="20"/> 
  4.     <column name="initial"/> 
  5. property> 

default属性为字段指定一个默认值 (在保存被映射的类的新实例之前,你应该将同样的值赋于对应的属性)。

  1. <property name="credits" type="integer" insert="false"> 
  2.     <column name="credits" default="10"/> 
  3. property> 
  4. <version name="version" type="integer" insert="false"> 
  5.     <column name="version" default="0"/> 
  6. property> 

sql-type属性允许用户覆盖默认的Hibernate类型到SQL数据类型的映射。

  1. <property name="balance" type="float"> 
  2.     <column name="balance" sql-type="decimal(13,3)"/> 
  3. property> 

check属性允许用户指定一个约束检查。

  1. <property name="foo" type="integer">   
  2.     <column name="foo" check="foo > 10"/>   
  3. property>   
  4. <class name="Foo" table="foos" check="bar < 100.0">   
  5.     ...    
  6.     <property name="bar" type="float"/>   
  7. class>  

【编辑推荐】

  1. 分析Hibernate插入操作
  2. 剖析Hibernate批量更新
  3. 全面讲解Hibernate二级缓存
  4. 讲述Hibernate核心接口
  5. 概括Hibernate批量处理
责任编辑:仲衡 来源: 7yue的博客
相关推荐

2009-06-26 10:12:00

Hibernate自动

2009-09-22 17:47:03

Hibernate s

2011-09-02 16:21:08

Sencha Touc自动生成工具

2009-09-22 09:31:15

Hibernate主键

2009-09-24 10:07:21

Hibernate M

2009-09-25 13:33:43

Hibernate主键

2009-07-16 11:40:23

ibatis自动生成abator

2009-06-25 17:24:06

Hibernate主键

2009-06-29 08:50:20

Hibernate主键

2016-08-04 11:15:24

GitHubMySQLSchema

2009-09-28 10:52:33

Hibernate主键主键生成

2009-09-23 13:21:32

Hibernate O

2009-07-14 17:12:26

ibatis自动代码生

2009-07-15 17:31:08

iBATIS Ecli

2009-09-24 16:22:50

Hibernate自动

2009-06-29 08:56:49

Hibernate主键生成主键

2009-09-22 13:25:54

Hibernate M

2009-07-16 11:21:19

ibatis主键自动生成

2019-11-05 18:50:37

代码开发工具

2009-06-26 16:21:22

Oracle自动增长Hibernate
点赞
收藏

51CTO技术栈公众号