Hibernate 配置
hibernate 配置
hibernate 需要知道在哪里找到映射信息,這些映射信息定義了 java 類怎樣關聯到數據庫表。
hibernate 也需要一套相關數據庫和其它相關參數的配置設置。所有這些信息通常是作為一個標準的 java 屬性文件提供的,文件名稱為 hibernate.properties
,或者使用xml 文件 hibernate.cfg.xml
。
我們將考慮 hibernate.cfg.xml
這個 xml 格式文件,來決定在我的例子里指定需要的 hibernate 應用屬性。這個 xml 文件中大多數的屬性是不需要修改的。這個文件保存在應用程序的類路徑的根目錄里。
hibernate 屬性
下面是一個重要的屬性列表,你可能需要表中的屬性來在單獨的情況下配置數據庫。
s.n. | 屬性和描述 |
---|---|
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 和數據庫應用程序服務器然后您必須配置以下屬性:
s.n. | 屬性和描述 |
---|---|
1 | hibernate.connection.datasource 在應用程序服務器環境中您正在使用的應用程序 jndi 名。 |
2 | hibernate.jndi.class jndi 的 initialcontext 類。 |
3 | hibernate.jndi.<jndipropertyname> 在 jndi的 initialcontext 類中通過任何你想要的 java 命名和目錄接口屬性。 |
4 | hibernate.jndi.url 為 jndi 提供 url。 |
5 | hibernate.connection.username 數據庫用戶名。 |
6 | hibernate.connection.password 數據庫密碼。 |
hibernate 和 mysql 數據庫
mysql 數據庫是目前可用的開源數據庫系統中最受歡迎的數據庫之一。我們要創建 hibernate.cfg.xml
配置文件并將其放置在應用程序的 classpath
的根目錄里。你要確保在你的 mysql 數據庫中 testdb 數據庫是可用的,而且你要有一個用戶 test 可用來訪問數據庫。
xml 配置文件一定要遵守 hibernate 3 configuration dtd,在 http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd. 這個網址中是可以找到的。
<?xml version="1.0" encoding="utf-8"?> <!doctype hibernate-configuration system "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect"> org.hibernate.dialect.mysqldialect </property> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.driver </property> <!-- assume test is the database name --> <property name="hibernate.connection.url"> jdbc:mysql://localhost/test </property> <property name="hibernate.connection.username"> root </property> <property name="hibernate.connection.password"> root123 </property> <!-- list of xml mapping files --> <mapping resource="employee.hbm.xml"/> </session-factory> </hibernate-configuration>
上面的配置文件包含與 hibernate-mapping
文件相關的 <mapping>
標簽,我們將在下章看看 hibernate mapping 文件到底是什么并且要知道為什么用它,怎樣用它。以下是各種重要數據庫同源語屬性類型的列表:
數據庫 | 方言屬性 |
---|---|
db2 | org.hibernate.dialect.db2dialect |
hsqldb | org.hibernate.dialect.hsqldialect |
hypersonicsql | org.hibernate.dialect.hsqldialect |
informix | org.hibernate.dialect.informixdialect |
ingres | org.hibernate.dialect.ingresdialect |
interbase | org.hibernate.dialect.interbasedialect |
microsoft sql server 2000 | org.hibernate.dialect.sqlserverdialect |
microsoft sql server 2005 | org.hibernate.dialect.sqlserver2005dialect |
microsoft sql server 2008 | org.hibernate.dialect.sqlserver2008dialect |
mysql | org.hibernate.dialect.mysqldialect |
oracle (any version) | org.hibernate.dialect.oracledialect |
oracle 11g | org.hibernate.dialect.oracle10gdialect |
oracle 10g | org.hibernate.dialect.oracle10gdialect |
oracle 9i | org.hibernate.dialect.oracle9idialect |
postgresql | org.hibernate.dialect.postgresqldialect |
progress | org.hibernate.dialect.progressdialect |
sap db | org.hibernate.dialect.sapdbdialect |
sybase | org.hibernate.dialect.sybasedialect |
sybase anywhere | org.hibernate.dialect.sybaseanywheredialec |