Hibernate 简明教程

Hibernate - ORM Overview

What is JDBC?

JDBC 代表 Java Database Connectivity 。它提供了一组 Java API,用于从 Java 程序访问关系数据库。这些 Java API 使 Java 程序能够执行 SQL 语句和与任何 SQL 兼容数据库进行交互。

JDBC stands for Java Database Connectivity. It provides a set of Java API for accessing the relational databases from Java program. These Java APIs enables Java programs to execute SQL statements and interact with any SQL compliant database.

JDBC 提供了一个灵活的架构来编写一个数据库独立的应用程序,该应用程序可以在不同的平台上运行并与不同的 DBMS 交互,而无需进行任何修改。

JDBC provides a flexible architecture to write a database independent application that can run on different platforms and interact with different DBMS without any modification.

Pros and Cons of JDBC

Pros of JDBC

Cons of JDBC

Clean and simple SQL processing Good performance with large data Very good for small applications Simple syntax so easy to learn

Complex if it is used in large projects Large programming overhead No encapsulation Hard to implement MVC concept Query is DBMS specific

Why Object Relational Mapping (ORM)?

当我们使用面向对象系统时,对象模型和关系数据库之间存在不匹配。RDBMS 以表格形式表示数据,而面向对象语言(例如 Java 或 C#)则以相互连接的对象图表的形式表示数据。

When we work with an object-oriented system, there is a mismatch between the object model and the relational database. RDBMSs represent data in a tabular format whereas object-oriented languages, such as Java or C# represent it as an interconnected graph of objects.

考虑以下具有适当构造函数和关联公共函数的 Java 类:

Consider the following Java Class with proper constructors and associated public function −

public class Employee {
   private int id;
   private String first_name;
   private String last_name;
   private int salary;

   public Employee() {}
   public Employee(String fname, String lname, int salary) {
      this.first_name = fname;
      this.last_name = lname;
      this.salary = salary;
   }

   public int getId() {
      return id;
   }

   public String getFirstName() {
      return first_name;
   }

   public String getLastName() {
      return last_name;
   }

   public int getSalary() {
      return salary;
   }
}

考虑将上述对象存储和检索到以下 RDBMS 表中:

Consider the above objects are to be stored and retrieved into the following RDBMS table −

create table EMPLOYEE (
   id INT NOT NULL auto_increment,
   first_name VARCHAR(20) default NULL,
   last_name  VARCHAR(20) default NULL,
   salary     INT  default NULL,
   PRIMARY KEY (id)
);

第一个问题是,如果在开发了一些页面或应用程序后需要修改数据库的设计,该怎么办?其次,在关系数据库中加载和存储对象会使我们面临以下五个不匹配问题:

First problem, what if we need to modify the design of our database after having developed a few pages or our application? Second, loading and storing objects in a relational database exposes us to the following five mismatch problems −

Sr.No.

Mismatch & Description

1

Granularity Sometimes you will have an object model, which has more classes than the number of corresponding tables in the database.

2

Inheritance RDBMSs do not define anything similar to Inheritance, which is a natural paradigm in object-oriented programming languages.

3

Identity An RDBMS defines exactly one notion of 'sameness': the primary key. Java, however, defines both object identity (a==b) and object equality (a.equals(b)).

4

Associations Object-oriented languages represent associations using object references whereas an RDBMS represents an association as a foreign key column.

5

Navigation The ways you access objects in Java and in RDBMS are fundamentally different.

*O*对象-*R*关系映射 (ORM) 是解决上述所有阻抗失配问题的办法。

The *O*bject-*R*elational *M*apping (ORM) is the solution to handle all the above impedance mismatches.

What is ORM?

ORM 代表 *O*对象-*R*关系映射 (ORM) 是一种用于转换关系数据库和诸如 Java、C# 等面向对象编程语言之间数据的编程技术。

ORM stands for *O*bject-*R*elational *M*apping (ORM) is a programming technique for converting data between relational databases and object oriented programming languages such as Java, C#, etc.

与纯 JDBC 相比,一个 ORM 系统有以下优点:

An ORM system has the following advantages over plain JDBC −

Sr.No.

Advantages

1

Let’s business code access objects rather than DB tables.

2

Hides details of SQL queries from OO logic.

3

Based on JDBC 'under the hood.'

4

No need to deal with the database implementation.

5

Entities based on business concepts rather than database structure.

6

Transaction management and automatic key generation.

7

Fast development of application.

一个 ORM 解决方案包含以下四个实体:

An ORM solution consists of the following four entities −

Sr.No.

Solutions

1

An API to perform basic CRUD operations on objects of persistent classes.

2

A language or API to specify queries that refer to classes and properties of classes.

3

A configurable facility for specifying mapping metadata.

4

A technique to interact with transactional objects to perform dirty checking, lazy association fetching, and other optimization functions.

Java ORM Frameworks

在 Java 中有几个持久框架和 ORM 选项。持久性框架是一种将对象存储到关系数据库并从关系数据库中检索对象的 ORM 服务。

There are several persistent frameworks and ORM options in Java. A persistent framework is an ORM service that stores and retrieves objects into a relational database.

  1. Enterprise JavaBeans Entity Beans

  2. Java Data Objects

  3. Castor

  4. TopLink

  5. Spring DAO

  6. Hibernate

  7. And many more