hbm2dll配置自動產生資料庫的幾點小結

來源:互聯網
上載者:User

一、hibernate資料庫自動產生的配置

(1)在hbm.cfg.xml中配置

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

(2)在applicaitonContext.xml中配置——與上面功能一樣

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

 

參數說明:

create : 會根據你的model類來產生表,但是每次運行都會刪除上一次的表,重建表,哪怕2次沒有任何改變
create-drop : 根據model類產生表,但是sessionFactory一關閉,表就自動刪除
update : 最常用的屬性,也根據model類產生表,即使表結構改變了,表中的行仍然存在,不會刪除以前的行
validate : 只會和資料庫中的表進行比較,不會建立新表,但是會插入新值

 

注意:

    很多朋友可能會遇到這樣的問題,明明是已經正確配置hbm2dll了,可就是不能正常自動產生資料庫。在這個時候,請檢查你的entity類的annotation配置或者hbm.xml配置。在*.hbm.xml配置當中,class節點需要根據不同的資料庫類型來配置屬性,一般情況下只需要如下配置就行

<class name="類名" table="表名">

有時可能還會用到sechema和catalog屬性。如:

<class name="entity.Educate" table="educate" schema="dbo" catalog="HR">

 

 

在這裡,另外附上hbm.cfg.xml的常用配置:

<!DOCTYPE hibernate-configuration PUBLIC<br /> "-//Hibernate/Hibernate Configuration DTD//EN"<br /> "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"></p><p><hibernate-configuration><br /> <session-factory></p><p> <!-- Settings for a local HSQL (testing) database. --><br /> <property name="dialect">org.hibernate.dialect.HSQLDialect</property><br /> <property name="connection.driver_class">org.hsqldb.jdbcDriver</property><br /> <property name="connection.url">jdbc:hsqldb:hsql://localhost</property><br /> <property name="connection.username">sa</property><br /> <property name="connection.password"></property></p><p> <!-- Use the C3P0 connection pool. --><br /> <property name="c3p0.min_size">3</property><br /> <property name="c3p0.max_size">5</property><br /> <property name="c3p0.timeout">1800</property></p><p> <!-- Disable second-level cache. --><br /> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property><br /> <property name="cache.use_query_cache">false</property><br /> <property name="cache.use_minimal_puts">false</property><br /> <property name="max_fetch_depth">3</property></p><p> <!-- Print SQL to stdout. --><br /> <property name="show_sql">true</property><br /> <property name="format_sql">true</property></p><p> <!-- Drop and then re-create schema on SessionFactory build, for testing. --><br /> <property name="hbm2ddl.auto">create</property></p><p> <!-- Bind the getCurrentSession() method to the thread. --><br /> <property name="current_session_context_class">thread</property></p><p> <!-- Hibernate XML mapping files --><br /> <mapping resource="org/MyClass.hbm.xml"/></p><p> <!-- Hibernate Annotations (and package-info.java)<br /> <mapping package="org.mypackage"/><br /> <mapping class="org.MyClass/><br /> --></p><p> </session-factory></p><p></hibernate-configuration><br />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.