[Hibernate系列—] 3. 映射文件和使用SchemaExport制作自己主动Schema

自己定义映射文件

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

相应的每一个数据库表栏位, 能够定义的属性有:

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

not-null

true or false

是否为空
unique

true or false

值是否唯一
indexstringThe name of a multi-column index
unique-keystringThe name of a multi-column unique constraint
foreign-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 column
checkstringAn 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 创建的project的 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 stub
		Configuration cfg = new Configuration().configure();
		new SchemaExport(cfg).create(true, true);		
	}

create 方法有两个參数:

1. 第一个參数设置是否输出DDL到控制台,
假设设置成true, 在控制台就能够看到下面相似的SQL

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





版权声明:本文博客原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/gcczhongduan/p/4717099.html