Implementation principle of The Hibernate framework ORM

Source: Internet
Author: User

1. What is Orm?

 

ORMObject Relational ing, That is, object relationship ing. Its implementation idea is to map the table data in the relational database to an object and present it as an object, so that developers can convert operations on the database to operations on these objects. Therefore, it aims to facilitate developers to operate databases with an object-oriented approach.

 

2.What isHibernate

 

There are many names for hibernate, such as tools, technologies, frameworks, and solutions. All of these are acceptable. What's important is that everyone needs to know its role. Here I often call it a framework,It is an implementationORMFramework. There are many frameworks that can implement the ORM function. hibernate can be said to be the most popular and developer-focused frameworks, and even the JBoss company has absorbed them, use it to implement the ORM function in your own project.

 

3. Implementation principle of ORM

 

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 combining it with Struts/spring and other frameworks as a solution for the entire system. Here I want to discuss some deeper topics, that is, how does hibernate IMPLEMENT THE ORM function? What should we do if we develop a framework that implements the ORM function on our own? 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 are:Ing Class (*. Java), Ing file (*. HBM. xml) And the database configuration file (*. PropertiesOr *. cfg. xml)And their respective functions are as follows.

 

(1)Ing Class: It is used to describe 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, includes mappings between ing classes and database tables, between table fields and class attribute types, and between table fields and Class Attribute names.

 

(3)Database Configuration File: It specifies the connection information required for database connection, such as the database to which the database is connected, logon username, logon password, and 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. To understand "ing", first we need to know how to parse these three types of files, that is, parsing XML format files, parsing properties format files, and parsing java files. Next we will discuss how to parse these files separately.

 

(1) howAnalysisXMLFile

 

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. I will briefly introduce the XML file format. 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 declarations, such as 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. XML file parsing function is very convenient, we can download some third-party toolkit such as xml-apis.jar and xercesimpl. jar, you can also use the built-in JDK tool class documentbuilderfactory,

Documentbuilder, document, element, and so on. You can refer to the descriptions of these classes through the API document. 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)AnalysisPropertiesFile

 

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 the configuration file uses the properties file description, we need to find a way to parse this type of file. To parse the properties file, you need to be familiar with the properties class. This class has some common methods, such as loading () to load the specified file and reading the attribute information in the file, propertynames () all property names are returned. 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 establish a connection with the database through the underlying JDBC technology.

 

(3)AnalysisJavaClass File

 

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. To map the data in a database table to an object, you must first retrieve the records in the table, and then assign each field value to each attribute of the ing Class Object, the value assignment process calls the Set Method in the object. Now we only know the class name and attribute name through the ing file. It is a key issue to call the corresponding set and get methods based on the class name and attribute name. In Java, there is a mechanism called reflection mechanism. Using this mechanism, we can obtain information about classes, this includes information such as modifiers, methods, attributes, inherited parent classes, and implementation interfaces used by the class. 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 attributes, methods, and constructor. You can use the getname (), GetType (), and getmodifiers () Methods of the field class to obtain the corresponding attribute name, attribute type, and attribute modifier information. The method class getreturntype () can be used to obtain the return type of the method. The invoke () method can execute the corresponding method in the 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 based on the method name to load the database data into the object.

 

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 JDBCDatabasemetadataClass andResultsetmetadataTo obtain the field name, type, size, and other information of the database table. Here, I just mentioned some key technologies for implementing the ORM function. Through the above introduction, we should have a rough UNDERSTANDING OF THE ORM mechanism, for example, you can obtain the database connection information and establish a connection by parsing the database configuration file, and obtain information such as the ing Class Name, attribute name, table name, and field name by parsing the ing file, after obtaining the name, you can obtain the ing Class information through the reflection mechanism. Call the constructor to create an object and call the Set Method of each attribute to set the value for the object to complete data loading. Hibernate uses this process to implement the Orm. Of course, Hibernate also usesTransaction Control,Cache ControlIf you are interested in many other technologies, we can discuss them later.

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.