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/

你可能感兴趣的:(数据结构,sql,maven,Hibernate,wordpress)