Hibernate要求预先知道在哪里可以找到它定义Java类是如何关联到数据库表的映射信息。 Hibernate也需要一组相关的数据库和其他相关参数的配置设置。所有这些信息通常是hibernate.properties,一个标准的Java属性文件,或者作为一个名为hibernate.cfg.xml的XML文件。
考虑XML格式的文件hibernate.cfg.xml中指定在例子必需的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. | 
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 |