Hibernate的配置檔案
阿新 • • 發佈:2018-11-29
對Ehibernate的核心配置檔案可以有兩種:
- hibernate.cfg.xml
- hibernate.properties
- 通過properties這種方式,不能配置<mapping>對映
- 編碼實現 Configuration config-new configuration ().addResource (xxx.hbm.xml) ;
核心配置檔案hibernate.cfg.xml檔案常用屬性
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">qaz123</property> <property name="hibernate.show_sql">true</property><!-- 顯示sql語句 --> <property name="hibernate.format_sql">true</property><!-- 格式化sql語句 -->
*DDL主要建表語句,和表結構更新語句
<property name="hibernate.hbm2ddl.auto ">create/create-drop/update/validate</property>
- screate表示啟動的時候先drop,再ceate (測試人員準備標準測試資料)
- create-drop:也表示建立,只不過再系統關閉前執行一下drop (測試程式是否正確)
- update:這個操作啟動的時候會去檢查表與類是否一致,如果不一致會做更新(建表,更新表結構【只能加】)
- validate:啟動時驗證現有表與你配置的hibernate是否一致,如果不一致就丟擲是常,並不做更新
**在產品開發電update和validate使用較多
<property name="hibernate.connection.autacommit">true</property>
設定事務自動提交.(有問題******.)
問題原因:如果設定成自動事務,執行後,通過session.flush ()就可以