- Hibernate 教程
- Hibernate - 首页
- ORM - 概述
- Hibernate - 概述
- Hibernate - 架构
- Hibernate - 环境
- Hibernate - 配置
- Hibernate - 会话
- Hibernate - 持久化类
- Hibernate - 映射文件
- Hibernate - 映射类型
- Hibernate - 示例
- Hibernate - O/R 映射
- Hibernate - 级联类型
- Hibernate - 注解
- Hibernate - 查询语言
- Hibernate - Criteria 查询
- Hibernate - 原生 SQL
- Hibernate - 缓存
- Hibernate - 实体生命周期
- Hibernate - 批处理
- Hibernate - 拦截器
- Hibernate - ID 生成器
- Hibernate - 保存图像
- Hibernate - log4j 集成
- Hibernate - Spring 集成
- Hibernate - Struts 2 集成
- Hibernate - Web 应用程序
- 映射表示例
- Hibernate - 表继承策略(每个层次结构一张表)
- Hibernate - 表继承策略(每个具体类一张表)
- Hibernate - 表继承策略(每个子类一张表)
- Hibernate 有用资源
- Hibernate - 问题与解答
- Hibernate - 快速指南
- Hibernate - 有用资源
- Hibernate - 讨论
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,则需要配置以下属性:
| 序号 | 属性及描述 |
|---|---|
| 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,该 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"
"https://hibernate.com.cn/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:///TUTORIALSPOINT</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>标签,这些标签与 hibernatemapping 文件相关,我们将在下一章中了解 hibernate 映射文件到底是什么以及我们如何以及为什么要使用它?
hbm2ddl.auto 属性
Hibernate 中的 hbm2ddl.auto 属性定义了如何处理数据库模式。可能的值有
create - 如果值为“create”,则在创建 SessionFactory 对象时,Hibernate 会在数据库中创建一个新表。如果数据库中存在同名的表,则会删除该表及其数据并创建一个新表。
update - 如果值为“update”,则 Hibernate 首先验证数据库中是否存在该表。如果存在,则根据更改修改该表。如果不存在,则创建一个新的表。
validate - 如果值为“validate”,则 Hibernate 只验证表是否存在。如果表不存在,则抛出异常。
create-drop - 如果值为“create-drop”,则在创建 SessionFactory 时,Hibernate 会创建一个新表,执行所需的操作,并在销毁 SessionFactory 时删除该表。此值用于测试 hibernate 代码。
none - 它不会对模式进行任何更改。
Hibernate 方言
数据库方言是一个配置选项,它允许软件将通用 SQL 语句转换为特定于供应商的 DDL 和 DML。不同的数据库产品,如 PostgreSQL、MySQL、Oracle 和 SQL Server,都有自己的 SQL 变体,称为 SQL 方言。
以下是各种重要数据库方言属性类型的列表:
| 序号 | 数据库及方言属性 |
|---|---|
| 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 - 也称为 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 关系数据库 (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 |