Hibernate ORM implementation principle

Source: Internet
Author: User

At present, we have a lot of discussions on hibernate in the Java field, such as its advantages and disadvantages, how to apply, how to solve the errors, and link it with Struts/spring and other frameworks.
Solutions for the entire system. Here I want to discuss some deeper topics, that is, how does hibernate IMPLEMENT THE ORM function? If we develop an implementation by ourselves
What is the framework of the ORM function? In fact, these problems are centered around the word "ing". If we know how to implement this ing, we can also develop our own ORM framework.
Developers who use hibernate know that when using it to implement the ORM function, the main files include: ing Class (*. Java), ing file (*. HBM. XML), and
Database Configuration Files (*. properties or *. cfg. XML) have the following functions.

(1) ing Class: it describes the structure of the database table. Fields in the table are described as attributes in the class. In the future, records in the table can be mapped to objects of this class.

(2) ing file: it specifies the relationship between database tables and ing classes, including ing Class and database table ing, table field and class attribute type ing, and table field and class attribute name
Mappings.

(3) Database Configuration File: it specifies the connection information required for database connection, such as the database to which the database is connected, the login user name, the login password, and the connection string.

Among the three main files, the ing Class is a common Java source file, the ing file is in XML format, the database configuration file is in properties format or XML format. Want to understand
"Ing" First, we need to know how to parse these three files, that is, parsing XML format files, parsing properties format files, and parsing Java class files. Next we will discuss it separately, as shown in figure
How to parse these files.

(1) how to parse XML files

As we mentioned earlier, the ing file is in XML format, and the database configuration file can also be in XML format. Therefore, if you can parse the XML file, you can obtain the information of the two files. XML file format
I will give a brief introduction. For example, Tom is a description in XML format. name indicates the node, and the node must have the Start mark and end mark, in the start tag, we can add some attribute declaration Ratio
For example, sex. XML parsing technology can be divided into two types: Sax and Dom. The differences and advantages and disadvantages of these two methods can be found online or we will discuss them in future articles. Please pay attention to them. Real
Now Parse XML file function is very convenient, we can download some third-party toolkit such as xml-apis.jar and xercesimpl. jar, you can also use JDK built-in
Tool class documentbuilderfactory, documentbuilder, document, element, etc.
Read the descriptions of these classes. Through these classes, we can read the XML file information into the memory and obtain the information about the specified node name, value, attribute name, and attribute value through some methods in the class.

(2) parse the properties File

The database configuration file can be in XML or properties format. The properties file generally describes the information in the form of "property name = property value. If you configure
The file uses the properties file description, and we need to find a way to parse this type of file. To parse the properties file, you must be familiar with the properties file.
Class, this class has some common methods such as load () loading the specified file and reading the attribute information in the file, propertynames () returns all attributes
Name, getproperty () returns the property value of the specified property name. By parsing the properties file, we can obtain the necessary information for connecting to the database, and then use the underlying JDBC technology
Establish a connection with the database.

(3) parsing java files

By parsing the ing file and the database configuration file, we can establish a database connection to obtain information such as the ing Class Name, attribute name, database table name, field name, and type. The
To map data to an object, you first need to retrieve the records in the table and then assign each field value to each attribute of the ing Class Object. In this assignment process, you need to call the Set Method in the object. Now, only
Knowing the class name and attribute name, how to call the corresponding set and get methods based on the class name and attribute name is a key issue. In Java, there is a mechanism called reflection mechanism. Using this mechanism, we can get the class
Information, including modifier, method, attribute, inherited parent class, and implementation interface. Classes related to reflection mechanisms include class, field, method, and
Constructor. Use the getfields (), getmethods (), and getconstructors () Methods of the class to obtain the corresponding owner.
And constructor. Use the getname (), GetType (), and getmodifiers () Methods of the field class to obtain the corresponding attribute name, attribute type, and attribute repair.
Ornaments. The method class getreturntype () can be used to obtain the return type of the method. The invoke () method can be used to obtain the corresponding response type in the execution object based on the given method name and parameter value.
. We can first get the attribute name in the class through the above method, then spell it into the setxxx and getxxx method names, and finally execute the corresponding method according to the method name, load the database data to
Image center.

In addition, the implementation of the hibernate mechanism involves a technical point, that is, how to obtain information about the database. To implement this function, you need to understand the JDBC
The databasemetadata class and the resultsetmetadata class can be used to obtain information about the field name, type, and size of a database table.
.

Here, I just mentioned some of the key techniques for implementing the ORM function. Through the above introduction, you should have a general understanding of the ORM mechanism, such as retrieving data by parsing the database configuration file.
Database connection information and establish a connection. By parsing the ing file, you can obtain information such as the ing Class Name, attribute name, table name, and field name. After obtaining the name, you can obtain the ing Class information through the reflection mechanism, create a call Constructor
Object, call the Set Method of each attribute to set the value of the object to complete data loading. Hibernate uses this process to implement Orm. Of course, Hibernate also uses transaction control.
System, Cache control, and many other technologies. If you are interested, we can discuss them later.

Due to the limited time, the introduction of some technologies mentioned above is relatively simple, and it may be difficult for some people to understand. It doesn't matter. I will introduce each technology in detail in the future, please pay attention,
At the same time, we warmly invite everyone to discuss and gradually analyze hibernate, so that we can take a closer look at hibernate's understanding and control capabilities. Thank you.

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.