现在的位置: 首页 > 综合 > 正文

hibernate hbb.xml 映射关系

2013年12月08日 ⁄ 综合 ⁄ 共 1412字 ⁄ 字号 评论关闭

本文我们主要介绍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"/>

抱歉!评论已关闭.