關於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>     
相關文章
相關標籤/搜索