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 一起使用,则必须配置以下属性 −
| 序号 | Properties & Description |
|---|---|
| 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,该文件可在 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>
上面的配置文件包含 <mapping> 标签,这些标签与 hibernatemapping 文件相关,我们将在下一章看到 hibernate 映射文件到底是什么以及我们如何以及为什么使用它?
以下是各种重要数据库语言属性类型的列表 −
| 序号 | 数据库 & 语言属性 |
|---|---|
| 1 |
DB2 org.hibernate.dialect.DB2Dialect |
| 2 |
HSQLDB org.hibernate.dialect.HSQLDialect |
| 3 |
HypersonicSQL org.hibernate.dialect.HSQLDialect |
| 4 |
Informix org.hibernate.dialect.InformixDialect |
| 5 |
Ingres org.hibernate.dialect.IngresDialect |
| 6 |
Interbase org.hibernate.dialect.InterbaseDialect |
| 7 |
Microsoft SQL Server 2000 org.hibernate.dialect.SQLServerDialect |
| 8 |
Microsoft SQL Server 2005 org.hibernate.dialect.SQLServer2005Dialect |
| 9 |
Microsoft SQL Server 2008 org.hibernate.dialect.SQLServer2008Dialect |
| 10 |
MySQL org.hibernate.dialect.MySQLDialect |
| 11 |
Oracle (any version) org.hibernate.dialect.OracleDialect |
| 12 |
Oracle 11g org.hibernate.dialect.Oracle10gDialect |
| 13 |
Oracle 10g org.hibernate.dialect.Oracle10gDialect |
| 14 |
Oracle 9i org.hibernate.dialect.Oracle9iDialect |
| 15 |
PostgreSQL org.hibernate.dialect.PostgreSQLDialect |
| 16 |
Progress org.hibernate.dialect.ProgressDialect |
| 17 |
SAP DB org.hibernate.dialect.SAPDBDialect |
| 18 |
Sybase org.hibernate.dialect.SybaseDialect |
| 19 |
Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialect |

