Hibernate - 配置
Hibernate 需要提前知道在哪里找到映射信息,这些信息定义了你的 Java class 与数据库表之间的关系。Hibernate 还需要一组与数据库和其他相关参数有关的配置设置。所有这些信息通常以标准的 Java properties 文件形式提供,名为 hibernate.properties,或者以名为 hibernate.cfg.xml 的 XML 文件形式提供。
在我的示例中,我将考虑使用 XML 格式的文件 hibernate.cfg.xml 来指定所需的 Hibernate properties。大多数 properties 都有默认值,除非确实需要,否则无需在 properties 文件中指定。该文件保存在应用程序 classpath 的根目录中。
Hibernate Properties
以下是在独立情况下为数据库配置所需的重要 properties 列表 −
| 序号 | Properties & 描述 |
|---|---|
| 1 |
hibernate.dialect 此 property 使 Hibernate 为所选数据库生成合适的 SQL。 |
| 2 | hibernate.connection.driver_class JDBC driver class。 |
| 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 连接使用 autocommit 模式。 |
如果你在应用程序服务器和 JNDI 环境中使用数据库,则需要配置以下 properties −
| 序号 | Properties & 描述 |
|---|---|
| 1 | hibernate.connection.datasource 在应用程序服务器上下文中定义的 JNDI 名称,用于你的应用程序。 |
| 2 | hibernate.jndi.class JNDI 的 InitialContext class。 |
| 3 | hibernate.jndi.<JNDIpropertyname> 将你喜欢的任何 JNDI property 传递给 JNDI InitialContext。 |
| 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,该 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://localhost/</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> 标签,这些标签与 hibernate mapping 文件相关,我们将在下一章详细了解 hibernate mapping 文件究竟是什么,以及我们如何以及为什么使用它。
hbm2ddl.auto 属性
Hibernate 中的 hbm2ddl.auto 属性定义了如何处理您的数据库 schema。可能的值包括:
create − 如果值为 'create',则在创建 SessionFactory 对象时,Hibernate 会在数据库中创建一个新表。如果数据库中已存在同名表,它会删除该表及其数据,并创建一个新表。
update − 如果值为 'update',Hibernate 会首先验证数据库中是否存在该表。如果存在,则根据变更修改该表;如果不存在,则创建一个新表。
validate − 如果值为 'validate',Hibernate 仅验证表是否存在。如果表不存在,则抛出异常。
create-drop − 如果值为 'create-drop',Hibernate 在创建 SessionFactory 时创建一个新表,执行所需操作,并在 SessionFactory 销毁时删除该表。此值用于测试 hibernate 代码。
none − 不对 schema 进行任何更改。
Hibernate Dialect
数据库方言(dialect)是一种配置选项,它允许软件将通用的 SQL 语句翻译成特定供应商的 DDL 和 DML。不同的数据库产品,如 PostgreSQL、MySQL、Oracle 和 SQL Server,都有自己的 SQL 变体,这些变体称为 SQL dialects。
以下是各种重要数据库方言属性类型的列表 −
| 序号 | 数据库 & Dialect 属性 |
|---|---|
| 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 - aka 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 Relational Database (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 |