关于Hibernate一个配置参数hibernate.hbm2ddl.auto

这两天在整理Spring + JPA(Hibernate实现),从网上copy了一段Hibernate链接参数的配置。
< 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的配置
后来更改了个人applicationContext.xml文件,
配置jpa以下,注意其相应位置的配置
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">        <property name="dataSource" ref="dataSource"></property>        <property name="jpaVendorAdapter">           <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">                   <property name="showSql" value="true" />             </bean>        </property>         <property name="jpaProperties">            <props>              <prop key="hibernate.hbm2ddl.auto">update</prop>            </props>         </property>      </bean>     
相关文章
相关标签/搜索