Hibernate参数配置说明表hibernate.hbm2ddl.auto

刚接触Hibernate,感觉特别的奇妙。因为可以利用工具进行逆向工程从数据库生成实体,不用自己写,多省功夫。。。o(∩_∩)o...

前几天拿着老师的例子,感觉特别的晕。。。因为老师演示的时候使用他自己的数据库给我们演示的哦。。。那。。。我们怎么运行啊???正郁闷时,竟然被我发现了在Hibernate.cfg.xml文件中还可以有如此一个属性。。。hbm2ddl.auto。。。呵呵。。。这下好了。。。原来Hibernate还有个正向工程。。。

收集了些网上的资料。。。o(∩_∩)o...

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

## auto schema export
#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).

create-drop

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

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.

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.

 

Mode

Reads

import.sql

Alters Database

Structure

Comments

update

No

Yes

 

create

Yes

Yes

Empties the database before creating it

create-drop

Yes

Yes

Drops the database when the SessionFactory is closed

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

Generating DDL Scripts with Maven

 

———————————————————————————————————————————————

<properties>
           <property name="hibernate.show_sql" value="true" />      
           <property name="hibernate.hbm2ddl.auto" value="create" />
</properties>

        结果在测试时,老是发现数据库表数据丢失。这个参数以前没怎么用,查了一圈其它的东东,最后才定位到这个上面。赶紧查了一下Hibernate的参数配置,解释如下:

hibernate.hbm2ddl.auto Automatically validate or export schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly. eg. validate | update | create | create-drop

其实这个参数的作用主要用于:自动创建|更新|验证数据库表结构。如果不是此方面的需求建议set value="none".

其它几个参数的意思,我解释一下:

validate               加载hibernate时,验证创建数据库表结构
create                  每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
create-drop         加载hibernate时创建,退出是删除表结构
update                加载hibernate自动更新数据库结构

总结:

1.请慎重使用此参数,没必要就不要随便用。

2.如果发现数据库表丢失,请检查hibernate.hbm2ddl.auto的配置