ORM implementation principle, orm implementation
1. What is ORM?
The full name of ORM is Object 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 is Hibernate?
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 a framework that can implement ORM. 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 (*. properties or *. cfg. xml). Their Respective functions are as follows.
(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, 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, 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. 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) 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. I will briefly introduce the XML file format. For example, <name sex = "male"> tom </name> is a description in XML format, with name representing the node, the node must have the start tag <name> and end tag </name>. 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. The ability to parse XML files is very convenient, we can download some third-party toolkit such as xml-apis.jar and xercesImpl. jar, etc. You can also use the tool class DocumentBuilderFactory, DocumentBuilder, Document, and Element that comes with JDK. You can refer to the description of these classes in 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) 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 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) 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. 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 JDBC DataBaseMetaData class and the ResultSetMetaData class. Through these two classes, you can obtain information about the field name, type, and size 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 ORM. Of course, Hibernate also uses many technologies such as transaction control and Cache control ......
Turn: http://www.cnblogs.com/bendantuohai/p/4711340.html
Thank you!