Hibernate Framework-01-01-Basic Introduction


Project address

https://yxmiaoyu.lanzous.com/b01c67olc Password: 74k5
https://yxmiaoyu.lanzous.com/b01c67olc
密码:74k5

Basic introduction

Three-tier architecture

Insert picture description here

Software architecture is to layer software.

Layered architecture

It refers to separating the components of the system into different layers, and the components in each layer should maintain cohesion; each layer should be loosely coupled with the layers below it.

  • The principle is high cohesion and low coupling
    . Cohesion: highly correlated within the class.
    Loose coupling: the correlation between layers should not be too close, and it is not easy to modify if it is too dependent

Persistence layer

In order to separate data access details from business logic, data access can be used as a separate persistence layer.

Insert picture description here

Endurance

Transient state : The program data saved in the memory, after the program exits, the data disappears, which is called the transient state.
Persistent state : the program data saved in the database (disk), after the program exits, the data still exists, which is called the persistent state of the program data.
Persistence : A mechanism for converting program data between transient state and persistent state.

狭义的理解:  “持久化”指把内存中的对象存储到关系型数据库
广义的理解:  “持久化” 包括和数据库相关的各种操作,保存、更新、删除、加载、查询

持久化层:   持久化层封装了数据访问细节,为业务逻辑层提供面向对象的API,
			使业务逻辑层可以专注于实现业务逻辑

Correspondence between MVC design pattern and four-layer structure

Insert picture description here

Design goals of the persistence layer:

  1. The code is highly reusable and can complete object persistence operations;
  2. If necessary, it can support multiple database platforms;
  3. With relative independence, when the persistence layer changes, it will not affect the upper layer implementation.

Software model

Analyze phase, create conceptual model,
design phase, create domain model and data model
Insert picture description here

Conceptual model

  1. Conceptual models are used to simulate real entities in the problem domain.
  2. The conceptual model describes the concepts and attributes of each entity, as well as the relationships between entities. (What are the entities, attributes and the relationships between them)
  3. The conceptual model does not describe the behavior of the entity.

Insert picture description here

Relational data model

The relational data model is established on the basis of the conceptual model and is used to describe the static structure of these relational data. It consists of the following contents:

  1. One or more tables;
  2. Referential integrity between tables; (foreign key)
  3. All indexes of the table;
  4. trigger;
  5. view.

Domain model

The domain model is object-oriented. In object-oriented terms, the domain model can also be called a design model.
The domain model consists of the following:

  1. Domain objects with state and behavior;
  2. The relationship between domain objects.
    Insert picture description here

Domain object

Domain objects can represent people, places, things, or concepts in the business domain. Domain objects are divided into the following categories:

  1. Entity domain object: the term of the business domain;
  2. Process domain objects: verbs in the business domain;
  3. Event domain objects: events in the business domain.
    Insert picture description here

Insert picture description here

There are many mismatches between the domain model and the relational model

  1. There are inheritance relationships in the domain model, and the relational model cannot directly express the inheritance relationship.
  2. There are many-to-many associations in the domain model, and the relational model expresses the many-to-many associations by joining tables.
  3. There are two-way association relationships in the domain model, and the relationship model has only one-way reference relationships, and the many side always refers to the one side.
  4. The domain model advocates the fine-grained model, and the relational model advocates the coarse-grained model.

Object-Relational Mapping-ORM

Object-Relational Mapping (ORM) is produced with the development of object-oriented software development methods. It is used to map the objects represented by the domain model to the database structure corresponding to the relational data model.

When operating entity objects through ORM mode, you don’t need to deal with complex SQL statements. You only need to simply manipulate the attributes and methods of entity objects. ORM technology provides a bridge between objects and relationships. Object type Data and relational data in the database are transformed into each other through this bridge.
Insert picture description here

Expand knowledge

In order to separate the business logic and data access details in the program, several ready-made patterns have emerged in the Java field.

Active domain mode, JDO mode, CMP mode, ORM mode, etc.

ORM mode refers to the persistence of all entity domain objects in a single component, and encapsulation of data access details. As long as the mapping relationship between the persistent class and the table is configured, the ORM middleware can refer to the information in the mapping file at runtime to persist the domain objects in the database.

ORM provides another mode for implementing the persistence layer. It uses mapping metadata to describe the details of object-relational mapping. Yes, ORM middleware can act as a bridge between the business logic layer of any Java application and the database. The main problem solved by ORM is object-relational mapping. The domain model is object-oriented, while the relational model is relation-oriented. Generally, a persistent class corresponds to a table, and each instance of the class corresponds to a record in the table. The following table lists the basic mapping between object-oriented concepts and relation-oriented concepts.

JDBC is cumbersome, troublesome to modify, redundant code, and large in duplication.

Hibernate

Hibernate is located in the persistence layer in the layered architecture, which is the persistence layer framework for object persistence;

Hibernate is a framework that connects Java applications and relational databases. It can establish a mapping between an object model and a relational data model. It is an automatic ORM framework;

Hibernate is a encapsulation of the JDBC API and a lightweight JDBC encapsulation framework.

Operating database in Java only has JDBC

What can Hibernate bring us?

  1. Hibernate implements ORM, which enables Java programmers to easily use object-oriented programming ideas to manipulate relational databases;

  2. Hibernate is a encapsulation of JDBC, which enhances the reusability of the code, simplifies the code, and improves the programming efficiency;

  3. Hibernate is a lightweight encapsulation of JDBC. If necessary, Java programmers can bypass Hibernate and directly access the JDBC API;

  4. Hibernate can be used not only in independent Java programs, but also in Java Web projects. It can be integrated with multiple web servers and supports multiple database platforms.

Guess you like

Origin blog.csdn.net/qq_44627608/article/details/114266355