Hibernate's hbm2ddl tool

格式:<prop key="hibernate.hbm2ddl.auto">update</prop>

## 自动schema输出
#hibernate.hbm2ddl.auto create-drop
#hibernate.hbm2ddl.auto create
#hibernate.hbm2ddl.auto update

create

Hibernate will create the database when the entity manager factory is created (actually when Hibernate's SessionFactory is created by the entity manager factory). If a file named import.sql exists in the root of the class path ('/import.sql') Hibernate will execute the SQL statements read from the file after the creation of the database schema. It is important to remember that before Hibernate creates the schema it empties it (delete all tables, constraints, or any other database object that is going to be created in the process of building the schema).

Hibernate将会在entity manager factory创建的时候建立数据库(实际上是在Hibernate的SessionFactory被entity manager factory创建的时候执行)。如果在class path中存在名为import.sql的文件(/import.sql),Hibernate将会在数据库架构创建之后读取sql文件中的SQL语句并执行。需要特别注意的是,在Hibernate创建爱你数据库结构之前,先要清空数据库(也就是删除所有的表、约束和所有其他的数据库对象之后再重新建立数据库结构)。

create-drop

Same as 'create' but when the entity manager factory (which holds the SessionFactory) is explicitly closed the schema will be dropped.

和"create”属性一样,不过在entity manager factory(用于管理SessionFactory的)被确认关闭后,schema将会被删除。

update

Hibernate creates an update script trying to update the database structure to the current mapping. Does not read and invoke the SQL statements from import.sql. Useful, but we have to be careful, not all of the updates can be done performed ? for example adding a not null column to a table with existing data.

Hibernate建立更新脚本,并尝试更新数据库结构到当前的映射中。不会从import.sql中读取并调用SQL语句。虽然会需要存在这种功能,但是我们必须十分小心,不是所有的更新都会被执行。例如向一个已经存在数据的表中添加一个不为空的字段。

validate

Validates the existing schema with the current entities configuration. When using this mode Hibernate will not do any changes to the schema and will not use the import.sql file.

通过当前的entities配置验证已经存在的schema。使用这种模式的时候,Hibernate将不会对schema做任何事情,并且不会用到import.sql文件。

模式

读取import.sql

更改数据库结构

注释

update

No

Yes

 

create

Yes

Yes

在创建之前必须先清空数据库

create-drop

Yes

Yes

当SessionFactory关闭以后删除数据库

validate

No

No

设hibernate.hbm2ddl.auto为create-drop/create后,在classpath中扔一个/import.sql进去,hibernate启动时就会执行import.sql的内容。设置为update时不执行import.sql.

http://www.jroller.com/eyallupu/entry/hibernate_s_hbm2ddl_tool

使用Maven生成DDL脚本(Generating DDL Scripts with Maven)
http://unmaintainable.wordpress.com/2007/06/30/generating-ddl-scripts-with-maven/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值