Hibernate 简明教程

Hibernate - Configuration

Hibernate 需要提前知道——在哪里找到映射信息,这些映射信息定义了 Java 类和数据库表之间的相互关系。Hibernate 还需要一组与数据库和其他相关参数相关的配置设置。所有此类信息通常按标准 Java 属性文件 hibernate.properties 提供,或按名为 hibernate.cfg.xml 的 XML 文件提供。

Hibernate requires to know in advance — where to find the mapping information that defines how your Java classes relate to the database tables. Hibernate also requires a set of configuration settings related to database and other related parameters. All such information is usually supplied as a standard Java properties file called hibernate.properties, or as an XML file named hibernate.cfg.xml.

我在示例中将考虑使用 XML 格式的文件 hibernate.cfg.xml 来指定必需的 Hibernate 属性。大多数属性采用其默认值,并且不必在属性文件中指定它们(除非确实需要)。此文件保存在应用程序类路径的根目录中。

I will consider XML formatted file hibernate.cfg.xml to specify required Hibernate properties in my examples. Most of the properties take their default values and it is not required to specify them in the property file unless it is really required. This file is kept in the root directory of your application’s classpath.

Hibernate Properties

以下是一些重要属性,在独立情况下,您需要对其进行数据库配置:

Following is the list of important properties, you will be required to configure for a databases in a standalone situation −

Sr.No.

Properties & Description

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 一起使用,则需要配置以下属性:

If you are using a database along with an application server and JNDI, then you would have to configure the following properties −

Sr.No.

Properties & Description

1

hibernate.connection.datasource The JNDI name defined in the application server context, which 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 with MySQL Database

MySQL 是当今最流行的开源数据库系统之一。我们将创建 hibernate.cfg.xml 配置文件,并将其放入应用程序类路径的根目录中。您必须确保您的 MySQL 数据库中存在 testdb 数据库,并且您有用户 test 可用于访问数据库。

MySQL is one of the most popular open-source database systems available today. Let us create hibernate.cfg.xml configuration file and place it in the root of your application’s classpath. You will have to make sure that you have testdb database available in your MySQL database and you have a user test available to access the database.

XML 配置文件必须符合 Hibernate 3 Configuration DTD,可以在 http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd 中获取该 DTD。

The XML configuration file must conform to the Hibernate 3 Configuration DTD, which is available at 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/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> 标记,这些标记与 Hibernate 映射文件相关,我们将在下一章中看到 Hibernate 映射文件确切是什么以及我们如何以及为什么使用它?

The above configuration file includes <mapping> tags, which are related to hibernatemapping file and we will see in next chapter what exactly a hibernate mapping file is and how and why do we use it?

hbm2ddl.auto Property

Hibernate 中的 hbm2ddl.auto 属性定义如何处理数据库架构。可能的值包括:

The hbm2ddl.auto property in Hibernate defines how your database schema is handled. Possible values are:

  1. create − If the value is 'create', Hibernate creates a new table in the database when the SessionFactory object is created. In case a table exists in the database with the same name, it deletes the table along with data and creates a new table.

  2. update − If the value is 'update', then Hibernate first validates whether the table is present in the database. If present, it alters that table as per the changes. If not, it creates a new one.

  3. validate − If the value is 'validate', then Hibernate only verifies whether the table is present. If the table does not exist then it throws an exception.

  4. create-drop − If the value is 'create-drop', then Hibernate creates a new table when SessionFactory is created, performs required operations, and deletes the table when SessionFactory is destroyed. This value is used for testing hibernate code.

  5. none − It does not make any changes to the schema.

Hibernate Dialect

数据库方言是一种配置选项,它允许软件将通用 SQL 语句转换到特定于供应商的 DDL 和 DML。不同的数据库产品(例如 PostgreSQL、MySQL、Oracle 和 SQL Server)有自己的 SQL 变体,称为 SQL 方言。

A database dialect is a configuration option that allows software to translate general SQL statements into vendor-specific DDL and DML. Different database products, such as PostgreSQL, MySQL, Oracle, and SQL Server, have their own variant of SQL, which are called SQL dialects.

以下是各种重要的数据库方言属性类型的列表 -

Following is the list of various important databases dialect property type −

Sr.No.

Database & Dialect Property

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 and later org.hibernate.dialect.Ingres9Dialect

13

Ingres 10 and later 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 (prior to 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