Hibernate配置問題

2021-09-08 11:14:34 字數 2049 閱讀 2706

hibernate是對持久化物件操作,生成sql語句達到運算元據庫目的。

1、hibernate可以通過兩種方式來配置

(1)、hibernate.cfg.xml,在此檔案裡hibernate-configuration-》session-factory-》property下可配置一些資料庫資訊,和自己特有的屬性

(2)、hibernate.properties,在此檔案裡以鍵值對(name-value)方式配置資料庫連線引數,但是無法直接配置資料庫表和類的對映檔案;可以通過編碼方式在**實現,如對映到檔案:configuration cfg = new configuration().addresource("item.hbm.xml").addresource("bid.hbm.xml");

對映類:

configuration

cfg = new configuration()

.addclass(org.hibernate.auction.item.class)

.addclass(org.hibernate.auction.bid.class);

第二種配置方式已過時,一般不使用。

2、hibernate核心配置檔案的屬性

必選屬性:

hibernate.dialect 運算元據庫方言,即指定哪種資料庫

hibernate.connection.driver.class 連線資料庫驅動程式

hibernate.connection.url 連線資料庫url

hibernate.connection.username 資料庫使用者名稱

hibernate.connection.password 資料庫密碼

可選屬性:

hibernate.show_sql true 顯示sql

hibernate.format_sql true 格式化sql

hibernate.hbm2ddl.auto create/create-drop/update/validate ddl策略,在hibernate.cfg.xml檔案中配置property,如:

create

create:表示啟動的時候先drop,再create;(一般用於測試人員準備測試資料)

create-drop:表示先create,在系統關閉前再drop;(一般用於測試人員準備測試資料)

update:這個操作啟動的時候會去檢查表與類是否一致,如果不一致則更新表,但是只能更新表結構的增加字段操作

validate:啟動時驗證現有表與配置的hibernate是否一致,不一致就丟擲異常,並不更新表結構

hibernate.connection.autocommit true/false 設定事務是否自動提交,預設false;(一般不配置)

3、hibernate configuration物件建立方式有兩種:

(1)、configuration config=new configuration();

預設是在classpath路徑下載入hibernate.properties配置檔案;注意:專案src下的檔案,缺省會被放到classpath路徑下

(2)、configuration config=new configuration().configure();

預設是在classpath路徑下載入hibernate.hbm.xml配置檔案

也可以呼叫含引數的configure方法載入指定的配置檔案,假如配置檔案更名為a.xml;則

4、手動載入對映配置檔案,了解即可,用的已不多

如:對映檔案:configuration cfg = new configuration().addresource("item.hbm.xml").addresource("bid.hbm.xml");

對映類:configuration cfg = new configuration().addclass(org.hibernate.auction.item.class).addclass(org.hibernate.auction.bid.class);

Hibernate配置問題

hibernate是對持久化物件操作,生成sql語句達到運算元據庫目的。1 hibernate可以通過兩種方式來配置 1 hibernate.cfg.xml,在此檔案裡hibernate configuration session factory property下可配置一些資料庫資訊,和自己特有的...

hibernate 常用配置

有些東西時間長了,經常想不起來 所以先記錄下來 1 hibernate.hbm2ddl.auto e.g.validat 執行前驗證表與實體是否一致 update 會話開始時,根據實體更新資料庫表 creat 會話開始時,重新建立資料庫的表 create drop 會話開始時候,建立資料庫中的表,關...

Hibernate方言配置

在開發hibernate的程式時,忽然用到了資料庫firebird,對於我來講不是很長用,忽然想到那麼多資料庫,方言都不一樣就再次記錄一下,以備後用。對於不同的資料庫,方言的值dialect是不同的,那麼下面就列出在不同的資料庫中如何設定該dialect值 如下 rdbms 方言db2 org.hi...