Home > Java > javaTutorial > body text

Detailed explanation of the basic principles of Hibernate in Java

黄舟
Release: 2017-09-09 11:01:02
Original
1562 people have browsed it

Persistence is a mechanism for converting program data between persistent and transient states. JDBC is a persistence mechanism. File IO is also a persistence mechanism. This article will introduce you to the basic principles of Hibernate in Java. Friends who need it can refer to it

Before starting to learn Hibernate, some people have always said: Hibernate is not difficult, it is nothing more than further encapsulation of JDBC. It's not difficult. Is it really not difficult, or is it just a matter of having too much ambition and too little ability?

If you just stay at the level of use, I believe that any technology is not difficult. See what others do, and you can get started quickly.

Is this the ultimate goal of learning?

No, absolutely not. We need to understand the basic concepts of this technology, what it is, what it does, and what its advantages and disadvantages are. Now I will lead you to review Hibernate together:

What is Hibernate?

Hibernate, translated as hibernation, has just entered autumn, and everything in the world is beginning to prepare for hibernation. In fact, for objects, it is persistence.​

​ Persistence (Persistence), that is, saving data (such as objects in memory) to a storage device (such as a disk) that can be saved permanently. The main application of persistence is to store objects in memory in relational databases. Of course, they can also be stored in disk files, XML data files, etc.

Persistence is a mechanism for converting program data between persistent and transient states.

JDBC is a persistence mechanism. File IO is also a persistence mechanism.

Daily persistence method:

#1. Refrigerate the fresh meat and defrost it when eating.

2. The same method is used to make canned fruits.

Let’s understand Hibernate from three perspectives:

1. Hibernate further encapsulates JDBC

It turns out that when Hiberante was not used for persistence layer development, there was a lot of redundancy, such as various JDBC statements and connection management. Therefore, Hibernate encapsulated JDBC and we did not need to manipulate data. Just operate it.

2. Let’s look at it from a layered perspective

We know the very typical three-tier architecture: presentation layer, business layer, and persistence layer. Hiberante is also a persistence layer framework, and there are many persistence layer frameworks, such as: IBatis, Nhibernate, JDO, OJB, EJB, etc.

3. Hibernate is an open source ORM (Object Relational Mapping) framework.

ORM, that is, Object-Relational Mapping, its function is to create a mapping between a relational database and objects. Mapping from Object to Relationship, and then from Relationship to Object. In this way, when we operate the database, we no longer need to deal with complex SQL, we only need to operate it like an object (mapping the fields of the relational database into the properties of the object in memory).

The core of Hibernate:

From the above picture, we can See the six core interfaces of Hibernate, the two main configuration files, and their direct relationship. Everything about Hibernate is here. Then let’s take a brief look at it from top to bottom and summarize each interface in one sentence.

1. Configuration interface: responsible for configuring and starting Hibernate

2. SessionFactory interface: responsible for initializing Hibernate

3. Session interface: responsible for CRUD operations of persistent objects

4. Transaction interface: responsible for transactions

5. Query interface and Criteria interface: responsible for executing various database queries

Note: Configuration instance is an object during startup , it is discarded once the SessionFactory is created.

Advantages/disadvantages of Hibernate:

## Advantages:

1. More object-oriented

Using object-oriented thinking to operate the database, we only need to operate objects, and development is more object-oriented.

2. Portability

Because Hibernate encapsulates the persistence layer, you don’t know the database, and all the code you write is reusable.

3. Hibernate is a non-invasive framework. We call non-invasive frameworks lightweight frameworks.

Comparing Action and ActionForm of Struts, both need to be inherited and cannot do without Struts. Hibernate does not need to inherit any classes or implement any interfaces. Such objects are called POJO objects.


4. Hibernate code testing is convenient.

5. Improve efficiency and productivity.

Disadvantages:

1. It will be difficult to tune statements using database features

2. There are problems with large batch data updates Problem

3. There are a large number of attack query functions in the system

Summary:

Hibernate allows us to use objectification The thinking of operating a relational database.

Summarize

The above is the detailed content of Detailed explanation of the basic principles of Hibernate in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template