
- Hibernate 教程
- Hibernate - 首頁
- ORM - 概述
- Hibernate - 概述
- Hibernate - 架構
- Hibernate - 環境
- Hibernate - 配置
- Hibernate - 會話
- Hibernate - 持久化類
- Hibernate - 對映檔案
- Hibernate - 對映型別
- Hibernate - 示例
- Hibernate - O/R 對映
- Hibernate - 級聯型別
- Hibernate - 註解
- Hibernate - 查詢語言
- Hibernate - Criteria 查詢
- Hibernate - 原生 SQL
- Hibernate - 快取
- Hibernate - 實體生命週期
- Hibernate - 批處理
- Hibernate - 攔截器
- Hibernate - ID 生成器
- Hibernate - 儲存影像
- Hibernate - log4j 整合
- Hibernate - Spring 整合
- Hibernate - Struts 2 整合
- Hibernate - Web 應用程式
- 對映表示例
- Hibernate - 表繼承策略(每個層次結構一張表)
- Hibernate - 表繼承策略(每個具體類一張表)
- Hibernate - 表繼承策略(每個子類一張表)
- Hibernate 有用資源
- Hibernate - 問題與解答
- Hibernate - 快速指南
- Hibernate - 有用資源
- Hibernate - 討論
Hibernate - 配置
Hibernate 需要預先知道在哪裡找到定義 Java 類如何與資料庫表相關聯的對映資訊。Hibernate 還需要一組與資料庫和其他相關引數相關的配置設定。所有這些資訊通常都作為名為hibernate.properties的標準 Java 屬性檔案或名為hibernate.cfg.xml的 XML 檔案提供。
在我的示例中,我將考慮使用 XML 格式的檔案hibernate.cfg.xml來指定所需的 Hibernate 屬性。大多數屬性都採用其預設值,並且除非確實需要,否則不需要在屬性檔案中指定它們。此檔案儲存在應用程式類路徑的根目錄中。
Hibernate 屬性
以下是您在獨立情況下為資料庫配置所需的重要屬性列表:
序號 | 屬性及描述 |
---|---|
1 |
hibernate.dialect 此屬性使 Hibernate 為所選資料庫生成適當的 SQL。 |
2 | hibernate.connection.driver_class JDBC 驅動程式類。 |
3 | hibernate.connection.url 到資料庫例項的 JDBC URL。 |
4 | hibernate.connection.username 資料庫使用者名稱。 |
5 | hibernate.connection.password 資料庫密碼。 |
6 | hibernate.connection.pool_size 限制在 Hibernate 資料庫連線池中等待的連線數。 |
7 | hibernate.connection.autocommit 允許對 JDBC 連線使用自動提交模式。 |
如果您正在使用資料庫以及應用程式伺服器和 JNDI,則需要配置以下屬性:
序號 | 屬性及描述 |
---|---|
1 | hibernate.connection.datasource 在您用於應用程式的應用程式伺服器上下文中定義的 JNDI 名稱。 |
2 | hibernate.jndi.class JNDI 的 InitialContext 類。 |
3 | hibernate.jndi.<JNDIpropertyname> 將您喜歡的任何 JNDI 屬性傳遞給 JNDI InitialContext。 |
4 | hibernate.jndi.url 提供 JNDI 的 URL。 |
5 | hibernate.connection.username 資料庫使用者名稱。 |
6 | hibernate.connection.password 資料庫密碼。 |
Hibernate 與 MySQL 資料庫
MySQL 是當今最流行的開源資料庫系統之一。讓我們建立hibernate.cfg.xml配置檔案並將其放置在應用程式類路徑的根目錄中。您需要確保您的 MySQL 資料庫中有testdb資料庫,並且您有一個名為test的使用者可以訪問該資料庫。
XML 配置檔案必須符合 Hibernate 3 Configuration DTD,該 DTD 可在http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd找到。
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 5.3//EN" "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hbm2ddl.auto">update</property> <property name="dialect">org.hibernate.dialect.MySQL8Dialect</property> <property name="connection.url">jdbc:mysql:///TUTORIALSPOINT</property> <property name="connection.username">root</property> <property name="connection.password">guest123</property> <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> <mapping resource="employee.hbm.xml"/> </session-factory> </hibernate-configuration>
上面的配置檔案包含<mapping>標籤,這些標籤與 hibernatemapping 檔案相關,我們將在下一章中瞭解 hibernate 對映檔案到底是什麼以及我們如何以及為什麼要使用它?
hbm2ddl.auto 屬性
Hibernate 中的 hbm2ddl.auto 屬性定義瞭如何處理資料庫模式。可能的值有
create - 如果值為“create”,則在建立 SessionFactory 物件時,Hibernate 會在資料庫中建立一個新表。如果資料庫中存在同名的表,則會刪除該表及其資料並建立一個新表。
update - 如果值為“update”,則 Hibernate 首先驗證資料庫中是否存在該表。如果存在,則根據更改修改該表。如果不存在,則建立一個新的表。
validate - 如果值為“validate”,則 Hibernate 只驗證表是否存在。如果表不存在,則丟擲異常。
create-drop - 如果值為“create-drop”,則在建立 SessionFactory 時,Hibernate 會建立一個新表,執行所需的操作,並在銷燬 SessionFactory 時刪除該表。此值用於測試 hibernate 程式碼。
none - 它不會對模式進行任何更改。
Hibernate 方言
資料庫方言是一個配置選項,它允許軟體將通用 SQL 語句轉換為特定於供應商的 DDL 和 DML。不同的資料庫產品,如 PostgreSQL、MySQL、Oracle 和 SQL Server,都有自己的 SQL 變體,稱為 SQL 方言。
以下是各種重要資料庫方言屬性型別的列表:
序號 | 資料庫及方言屬性 |
---|---|
1 | Caché 2007.1 org.hibernate.dialect.Cache71Dialect |
2 | DB2 org.hibernate.dialect.DB2Dialect |
3 | DB2/390 org.hibernate.dialect.DB2390Dialect |
4 | DB2/400 org.hibernate.dialect.DB2400Dialect |
5 | Cloudscape 10 - 也稱為 Derby。 org.hibernate.dialect.DerbyDialect |
6 | Firebird org.hibernate.dialect.FirebirdDialect |
7 | FrontBase org.hibernate.dialect.FrontBaseDialect |
8 | H2 org.hibernate.dialect.H2Dialect |
9 | HSQLDB(HyperSQL) org.hibernate.dialect.HSQLDialect |
10 | Informix org.hibernate.dialect.InformixDialect |
11 | Ingres 9.2 org.hibernate.dialect.IngresDialect |
12 | Ingres 9.3 及更高版本 org.hibernate.dialect.Ingres9Dialect |
13 | Ingres 10 及更高版本 org.hibernate.dialect.Ingres10Dialect |
14 | Interbase org.hibernate.dialect.InterbaseDialect |
15 | Microsoft SQL Server 2000 org.hibernate.dialect.SQLServerDialect |
16 | Microsoft SQL Server 2005 org.hibernate.dialect.SQLServerDialect |
17 | Microsoft SQL Server 2008 org.hibernate.dialect.SQLServer2008Dialect |
18 | MySQL(5.x 之前) org.hibernate.dialect.MySQLDialect |
19 | MySQL 5.x org.hibernate.dialect.MySQL5Dialect |
20 | Oracle 8i org.hibernate.dialect.Oracle8iDialect |
21 | Oracle 9i org.hibernate.dialect.Oracle9iDialect |
22 | Oracle 10g org.hibernate.dialect.Oracle10gDialect |
23 | Oracle 11g org.hibernate.dialect.Oracle10gDialect |
24 | Pointbase org.hibernate.dialect.PointbaseDialect |
25 | PostgreSQL org.hibernate.dialect.PostgreSQLDialect |
26 | PostgreSQL Plus org.hibernate.dialect.PostgrePlusDialect |
27 | Progress org.hibernate.dialect.ProgressDialect |
28 | Unisys 2200 關係資料庫 (RDMS) org.hibernate.dialect.RDMSOS2200Dialect |
29 | SAP DB org.hibernate.dialect.SAPDBDialect |
30 | Sybase 11.9.2 org.hibernate.dialect.Sybase11Dialect |
31 | Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialect |
32 | Sybase Adaptive Server Enterprise (ASE) 15 org.hibernate.dialect.SybaseASE15Dialect |
33 | Teradata org.hibernate.dialect.TeradataDialect |
34 | TimesTen 5.1 org.hibernate.dialect.TimesTenDialect |