posts - 32,  comments - 3,  trackbacks - 0

<property name="hibernate.hbm2ddl.auto" value="update" />

Hibernate Reference Documentation解释如下:
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

这个参数的作用主要用于:自动创建|更新|验证数据库表结构。
如果没有此方面的需求建议不设置此属性,默认是不对数据库结构作任何改变

其它几个参数的意思:
validate     
每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。

create
每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。

create-drop
每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。

update
最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。

posted on 2011-04-17 16:53 donghang73 阅读(538) 评论(0)  编辑  收藏 所属分类: 学习笔记

只有注册用户登录后才能发表评论。


网站导航: