Hibernate配置
Hibernate要求預先知道在哪裡可以找到它定義Java類是如何關聯到數據庫表的映射信息。 Hibernate也需要一組相關的數據庫和其他相關參數的配置設置。所有這些信息通常是hibernate.properties,一個標準的Java屬性文件,或者作為一個名為hibernate.cfg.xml的XML文件。
考慮XML格式的文件hibernate.cfg.xml中指定在例子必需的Hibernate屬性。大部分的屬性取默認值,它不需要在屬性文件中指定它們,除非它真的是必需的。此文件保存在應用程序的類路徑的根目錄。
Hibernate 屬性:
以下是需要在獨立情況下配置一個數據庫的重要屬性的列表:
S.N. | 屬性和說明 |
---|---|
1 |
hibernate.dialect This property makes Hibernate generate the appropriate SQL for the chosen database. |
2 |
hibernate.connection.driver_class The JDBC driver class. |
3 |
hibernate.connection.url The JDBC URL to the database instance. |
4 |
hibernate.connection.username The database username. |
5 |
hibernate.connection.password The database password. |
6 |
hibernate.connection.pool_size Limits the number of connections waiting in the Hibernate database connection pool. |
7 |
hibernate.connection.autocommit Allows autocommit mode to be used for the JDBC connection. |
如果是隨著應用程序服務器和JNDI使用一個數據庫,那麼就必須配置以下屬性:
S.N. | 屬性和說明 |
---|---|
1 |
hibernate.connection.datasource The JNDI name defined in the application server context you are using for the application. |
2 |
hibernate.jndi.class The InitialContext class for JNDI. |
3 |
hibernate.jndi.<JNDIpropertyname> Passes any JNDI property you like to the JNDI InitialContext. |
4 |
hibernate.jndi.url Provides the URL for JNDI. |
5 |
hibernate.connection.username The database username. |
6 |
hibernate.connection.password The database password. |
Hibernate和MySQL數據庫:
MySQL是最受歡迎的開源數據庫係統之一。讓我們創建hibernate.cfg.xml配置文件,並將其放置在應用程序的類的根路徑。必須確保有MySQL數據庫可供TESTDB數據庫,必須提供一個用戶test來訪問數據庫。
XML配置文件必須符合Hibernate 3配置的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>標簽,我們將在下一章看到到底什麼是Hibernate映射文件,以及如何和為什麼要使用它。以下是各種重要的數據庫方言屬性類型的列表:
Database | Dialect Property |
---|---|
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.SybaseAnywhereDialect |