[Hibernate系列—] 3. 映射文件与使用SchemaExport自动产生Schema

来源:互联网 发布:linux移动目录命令 编辑:程序博客网 时间:2024/06/01 08:15

自定义映射文件

这里的映射文件指的是对应到数据库表的xml 的定义文件。

对应的每个数据库表栏位, 可以定义的属性有:

属性名类型Descriptionlengthnumber栏位的长度precisionnumberprecision表示数字中的有效位。如果没有指定precision的话,Oracle将使用38作为精度scalenumberscale表示数字小数点右边的位数,scale默认设置为0.  如果把scale设成负数,Oracle将把该数字取舍到小数点左边的指定位数。not-null

true or false

是否为空unique

true or false

值是否唯一indexstringThe name of a multi-column indexunique-keystringThe name of a multi-column unique constraintforeign-keystringThe name of the foreign key constraint generated for an association. This applies to <one-to-one>, <many-to-one>, <key>, and <many-to-many> mapping elements. inverse="true" sides are skipped by SchemaExport.sql-typestringOverrides the default column type. This applies to the <column> element only.defaultstringDefault value for the columncheckstringAn SQL check constraint on either a column or atable

设置步骤

1. 设置 映射元素的length, precision 和 scale

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

2. 设置 not-null, UNIQUE, unique-key

not-null 如为true,指名该字段不允许为null,默认false

unique 如为true,指名该字段具有唯一约束,默认false

unique-key 为多个字段设定唯一约束

<many-to-one name="bar" column="barId" not-null="true"/><element column="serialNumber" type="long" not-null="true" unique="true"/><many-to-one name="org" column="orgId" unique-key="OrgEmployeeId"/><property name="employeeId" unique-key="OrgEmployee"/>

3. 设置 index 和 foreign-key 

foreign-key 为外键约束命名,在<many-to-many><one-to-one><key><many-to-one>元素中包含
foreign-key属性,在双向关联中,inverse属性为true的一端不能设置foreign-key

ndex 给一个或多个字段建立索引

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

4.  设置 child 元素

当有多个数据库栏位组成一个类的属性

<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>

5. 设置默认值

<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"/></version>

6. 设置 sql-type

sql-type 设定字段sql类型

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

7. 设置 check

check 设定sql检查约束

<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>

8. 添加注释

<class name="Customer" table="CurCust">  <comment>Current customers only</comment>  ...</class>


使用SchemaExport 工具产生数据库DDL 脚本


[Hibernate系列—] 1. 下载与试用Hibernate(MySQL与Oracle 配置)

这一篇的Usr.hbm.xml 为例,介绍如何使用命名行的方式产生DDl.

 cmd 到 Eclipse 创建的工程的 bin 目录下,

执行:

java -cp .;../lib/hibernate-core-4.3.5.Final.jar;../lib/jboss-logging-3.1.3.GA.jar;../lib/jboss-transaction-api_1.2_spec-1.0.0.Final.jar;../lib/dom4j-1.6.1.jar;../lib/hibernate-commons-annotations-4.0.4.Final.jar;../lib/ojdbc14.jar;../lib/javassist-3.18.1-GA.jar;../lib/jandex-1.1.0.Final.jar;../lib/hibernate-jpa-2.1-api-1.0.0.Final.jar org.hibernate.tool.hbm2ddl.SchemaExport --config=hibernate.cfg.xml --output=my_schema.ddl
可以看到产生的  my_schema.ddl 文件的内容如下:




代码方式产生DDL脚本并执行

除了使用命令行外, 还可以使用代码的方式产生DDL 并执行这个DDL.

在bibernate.cfg.xml 配置好的前提下, 运行以下代码:

public static void main(String[] args) {// TODO Auto-generated method stubConfiguration cfg = new Configuration().configure();new SchemaExport(cfg).create(true, true);}

create 方法有两个参数:

1. 第一个参数设置是否输出DDL到控制台,
如果设置成true, 在控制台就可以看到以下类似的SQL

2. 第二个参数设置是否要在Db 中执行这个SQL





0 0