[Hibernate系列—] 3. 映射文件与使用SchemaExport自动产生Schema
来源:互联网 发布:linux移动目录命令 编辑:程序博客网 时间:2024/06/01 08:15
自定义映射文件
这里的映射文件指的是对应到数据库表的xml 的定义文件。
对应的每个数据库表栏位, 可以定义的属性有:
true
or false
true
or false
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
- [Hibernate系列—] 3. 映射文件与使用SchemaExport自动产生Schema
- hibernate使用SchemaExport自动生成数据表
- Hibernate 使用SchemaExport自动生成表
- Hibernate之——使用SchemaExport类自动创建数据库表
- 使用MiddleGen 产生hibernate的数据库表映射文件
- 使用MiddleGen 产生hibernate的数据库表映射文件
- 使用MiddleGen 产生hibernate的数据库表映射文件
- 使用MiddleGen 产生hibernate的数据库表映射文件
- 使用MiddleGen 产生hibernate的数据库表映射文件
- 使用MiddleGen 产生hibernate的数据库表映射文件
- 使用MiddleGen 产生hibernate的数据库表映射文件
- 使用MiddleGen产生hibernate的数据库表映射文件
- 使用MiddleGen 产生hibernate的数据库表映射文件
- 使用MiddleGen 产生hibernate的数据库表映射文件
- 【Hibernate】——SchemaExport自动生成数据库表
- 使用 SchemaExport 自动建表
- 使用 SchemaExport 自动建表
- 使用 SchemaExport 自动建表
- Swift编程语言入门实战系列教程(五)---增加键盘输入函数
- 剑指offer面试题32
- sqlite操作
- Object literals新特性(非原创)
- hadoop2.2.0源码编译笔记
- [Hibernate系列—] 3. 映射文件与使用SchemaExport自动产生Schema
- CSocket类的Receive超时的问题解决方案
- java基础学习
- 14. 多线程
- 2014.03.16 网易游戏TTT计划实习生笔试题
- 简单的wcf服务
- VPN中转服务器方案
- VC,VB程序按钮、图标样式美化
- windows查看线程工具