[Java EE] Hibernate ORM

Source: Internet
Author: User

I. A brief introduction to Hibernate

Hibernate is a durable layer solution for lightweight Java EE applications , and hibernate not only manages Java class-to-database table mappings (including Java data types to SQL data type mappings), but also provides methods for querying data and fetching data. The time to process data persistence with JDBC can be significantly shortened.

The current mainstream database is still a relational database, while the Java language is an object-oriented programming language, when the combination of the two is quite troublesome, and hibernate reduces the problem, it completes the object model and SQL-based relational model mapping relationship.

Hibernate is more object-oriented than the frame ibatis,hibernate of another persistence layer, and Hibernate uses a low-intrusive design compared to the traditional entity EJB scheme, that is, using common Java Objects (POJO). Instead of inheriting a superclass of Hibernate or implementing an interface for Hibernate. Hibernate is a bridge between object-oriented programming languages and relational databases, and hibernate allows developers to manipulate relational databases using an object-oriented approach.

With hibernate support, the OOA (object-oriented analysis), OOD (object-oriented design) and OOP (object-oriented programming) of the Java EE application are all three processes same strain to become a whole.

two. ORM

Currently popular programming languages, such as Java, C #, are all object-oriented programming languages, and the current mainstream database products such as Oracle, DB2, etc., are still relational databases. The uncoordinated development of programming languages and underlying databases has spawned an ORM framework. ORM Framework can be used as a bridge between object-oriented language and relational database.

1. Object/relational database mapping (ORM)

The full name of the ORM is Object/relation Mapping, object/relational database mapping. ORM can be understood as a specification that outlines the basic characteristics of such frameworks: the mapping of object-oriented programming languages and relational databases. When the ORM framework completes the mapping, it can take advantage of the simple usability of the object-oriented programming language and the technical advantages of the relational database. Therefore, we can think of the ORM framework as a bridge between applications and databases.

When we use an object-oriented programming language for application development, from the beginning of the project is the face of object analysis, object-oriented design, object-oriented programming, but to the persistence of database access, but also to return to the relationship of the library access mode, this is a bad feeling. So we need a tool that can wrap a relational database into an object-oriented model, which is the ORM framework.

ORM is an intermediate solution for the development of object-oriented programming language and relational database in different stages. With the development of object-oriented database, the theory is gradually perfected, which will eventually replace the relational database. But this process will not happen overnight, and the ORM framework will flourish during this period. But with the advent of the object database, the ORM tool will automatically die out.

For the popular programming language, object-oriented programming language represents the current programming language mainstream and trend, with a lot of advantages. Like what:

(1) Object-oriented modeling, operation

(2) Polymorphism, inheritance

(3) to abandon the difficult process of understanding

(4) Easy to use and easy to understand

But the development of the database is not synchronized with the programming language, and some advantages of the relational database are incomparable to the object-oriented language. Like what:

(1) Find and sort large amounts of data

(2) Collection data connection operation, mapping

(3) Concurrency, transaction of database access

(4) Database constraints and Isolation

When we adopt the ORM framework, the application no longer accesses the underlying database directly, but rather the object-oriented approach to persisting objects (such as creation, modification, deletion, etc.), while the ORM framework translates these object-oriented operations into the underlying SQL operations.

As the only function of the ORM tool shown is to transform the operation of the persisted object into a database operation, the programmer can manipulate the persisted object in an object-oriented manner, while the ORM Framework is responsible for translating the corresponding SQL (Structured Query Language) operations.

2. Basic Mapping Method

The ORM tool provides a mapping between a persisted class and a data table, and through the mapping of this relationship, we can easily manipulate the data table through the persistence class. In fact, all ORM tools follow the same mapping approach in general.

The ORM Basic mapping has the following mapping relationships:

(1) Data Table mapping class :

The persisted class is mapped to a data table. When we use this persistence class to create instances, modify properties, and delete instances, the system automatically translates to CRUD operations on the table, showing this mapping.

As shown, the ORM-managed persistence class (which is an ordinary Java class) corresponds to a data table, and as long as we operate on the persisted class, the system can be converted to the operation of the corresponding data table.

(2) The row map object (that is, the instance) of the data table:

The persistence class generates many instances, each of which corresponds to a row of records in the data table. When we modify an instance of a persisted class in the app, the ORM tool translates to the operation of a particular row in the corresponding data table. Each persisted object corresponds to a row of records for the data table. As shown in the following:

(3) The properties of the data table's column (field) Map object:

When we modify the setting properties of a persisted object in the app (persisted data maps to data rows), the ORM is converted to the operation of the specified data row, specified column in the corresponding table. The data table columns are mapped to the following representations of the object properties:

Based on these basic mapping methods, ORM tools can accomplish the mapping between the object model and the relational model. Thus, in an ORM Framework, object persistence is an intermediary in which an application only needs to manipulate persisted objects, and the ORM Framework is responsible for translating this into the underlying database operations--a transformation that is transparent to the developer and frees developers from the relational model, Enables developers to manipulate relational databases with object-oriented thinking.

[Java EE] Hibernate ORM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.