📜  Hibernate-ORM概述

📅  最后修改于: 2020-11-16 06:55:39             🧑  作者: Mango


什么是JDBC?

JDBC代表Java数据库连接。它提供了一组Java API,用于从Java程序访问关系数据库。这些Java API使Java程序能够执行SQL语句并与任何符合SQL的数据库进行交互。

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

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

为什么选择对象关系映射(ORM)?

当我们使用面向对象的系统时,对象模型和关系数据库之间会出现不匹配的情况。 RDBMS以表格格式表示数据,而面向对象的语言(例如Java或C#)则将其表示为对象的互连图。

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

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表中-

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)
);

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

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 bject-řelational中号apping(ORM)是处理所有上述阻抗失配的溶液。

什么是ORM?

ORM代表O bject-řelational中号apping(ORM)是用于将定向关系数据库和对象之间的数据的编程语言,例如Java,C#等一种编程技术

与普通JDBC相比,ORM系统具有以下优点-

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解决方案由以下四个实体组成-

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框架

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

  • 企业JavaBean实体Bean
  • Java数据对象
  • 脚轮
  • 顶联
  • 春道
  • 冬眠
  • 还有很多