Original link: http://blog.163.com/hzd_love/blog/static/13199988120107891854473/
1. What is ORM
The full name of the ORM is the object Relational Mapping, which is a relational mapping of objects. The idea of its realization is to map the data of tables in the relational database into objects, so that the developers can transform the operation of the database into the operation of the objects. So it is designed to facilitate the development of the object-oriented thinking to achieve the operation of the database.
2. What is Hibernate
There are many names for hibernate, such as tools, techniques, frameworks, and solutions, all of which are important to know. Here I habitually call it framework, it is a kind of framework that can implement ORM. There are many frameworks to implement ORM, and hibernate can be said to be the most popular and developer-focused of these frameworks, and even JBoss has absorbed it, using it to implement ORM functionality in its own projects.
3. The implementation principle of ORM
There are many discussions about hibernate in the Java field, such as its advantages and disadvantages, how to apply it, how to solve it, and how to combine it with struts/spring and other frameworks as a solution for the whole system. Here I would like to discuss with you some deeper topic, that is hibernate is how to achieve the function of ORM? What do we need to do to develop a framework that implements ORM functionality? In fact, these problems revolve around a word, that is, "mapping", if we know how to implement this mapping then we can also develop their own ORM framework. Developers who use hibernate know that when using it to implement ORM functionality, the main files are: the mapping Class (*.java), the mapping file (*.hbm.xml), and the database configuration file (*.properties or *. Cfg.xml), their respective functions are as follows.
⑴ mapping class: Its role is to describe the structure of the database table, the table fields are described as attributes in the class, in the future can be implemented to map the table records into the object of the class.
⑵ mapping File: Its role is to specify the relationship between the database table and the mapping class, including mapping class and database table correspondence, table field and Class property type corresponding relationship, and table field and class property name correspondence.
⑶ Database configuration file: It is to specify connection information that is required to connect to the database, such as which database to connect to, login user name, login password, and connection string.
In these three main files, the mapping class is a normal Java source file, the mapping file is in XML format, the database configuration file is the properties format, or is an XML format. To understand "mapping" first we need to know how to parse these three files, parsing the XML format file, parsing the properties format file, and parsing the Java class file.
Let's explore how to implement the parsing of these files separately.
⑴ How to parse an XML file
As we said earlier, the mapping file is in XML format, and the database configuration file can be in XML format, so if we can parse the XML file we can get the information of the two files. XML file format I briefly introduce, for example <name sex= "Male" >tom</name> this is a description of the XML format, name represents 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. Parsing XML technology can be divided into two categories that is sax and DOM, the differences and advantages and disadvantages of these two methods can be accessed online or we will put forward in future articles, please pay attention to. The ability to parse XML files is convenient, we can download some third-party toolkits such as Xml-apis.jar and Xercesimpl.jar, or use the JDK's own tool class Documentbuilderfactory, Documentbuilder, Document, element and so on, you can use the API document to check the description of these classes. Through these classes we can read the information of the XML file into memory and get the information of the specified node's name, value, property name, property value through some methods in the class.
⑵ Parsing properties File
The database configuration file can be either XML or properties, and the properties file typically describes the information in the form of a property name = attribute value. If the configuration file is described by the properties file, 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 of this class, this class has some common methods for example, load () load the specified file and read the file property information, PropertyNames () return all property names, GetProperty () Returns the property value for the specified property name. By parsing the properties file we can get the necessary information to connect to the database and then establish a connection with the database through the underlying JDBC technology.
⑶ parsing Java class files
By parsing the mapping file and the database configuration file, we can establish the connection of the database, and we can get the information such as the name, property name, database table name, field name and type of the mapping class. To map data from a table in a database into an object, you first need to take the records in the table and then give each field value to each property of the mapped class object, which calls the Set method in the object. It is a critical problem that we now know only the class name and property name through the mapping file, and how to invoke the appropriate set and get methods based on the class name and property name. In Java there is a mechanism called the reflection mechanism, using which we can get information about the class, including the modifiers, methods, attributes, inherited parent classes, and implementation interfaces. The classes related to the reflection mechanism are class, Field, method, constructor, and so on. The GetFields (), GetMethods () and GetConstructors () Methods of class are used to obtain the corresponding properties, methods and construction methods. The corresponding property name, property type, and property modifier information are obtained through the GetName (), GetType (), and GetModifiers () Methods of the field class. The return type of the method can be obtained by means class Getreturntype (), and the Invoke () method executes the corresponding method in the object based on the given method name and parameter value. We can first get the property names in the class by using the above method, then spell the setxxx and getxxx method names, and finally execute the corresponding method according to the method name to load the database data into the object.
In addition, to implement the hibernate mechanism involves a technical point, that is, how to obtain information about the database. To realize this function, we need to understand the DatabaseMetaData class and the ResultSetMetaData class of JDBC, through these two classes we can get the database table field name, type, size and other related information.
Here I just give you some of the implementation of the ORM function of some key technologies, through the above introduction to the ORM mechanism should have a general understanding, such as through the analysis of database configuration file to obtain database connection information and establish a connection, through the resolution map file can get the mapping class name, property name, Information such as table name and field name can be obtained by the reflection mechanism to obtain the mapping class information, call the construction method to create the object, call the set method of each property to set the value of the object to complete the loading of data. Hibernate is the process used to achieve ORM, of course, Hibernate also uses the transaction control, cache control and many other technologies
How ORM (Object-relational Mapping Object-relational mapping) is implemented (GO)