Part 6: basic implementation principles

Source: Internet
Author: User

Overall process
1: Read the cfg. xml file through configuration
2: Get the SessionFactory
3: Use the SessionFactory factory to create a Session instance
4: open a transaction through Session
5. Operate the database through the session api
6: transaction commit
7. Close the connection.
 
Note: The implementation process described in the following methods is not the complete implementation process of Hibernate, nor the complete implementation sequence of Hibernate. It only describes the backbone and basic methods for Hibernate to implement these methods, it is mainly used to understand what is happening behind these methods. If you need a detailed and complete implementation process, please refer to the relevant documentation and source code of Hibernate.

After session. save (UserModel) is called:
1: TO ---> PO: Hibernate first searches in the cache. If a PO with the same id already exists in the internal cache, the data is saved and an exception is thrown.
If the cache does not exist, Hibernate will put the TO object passed in TO the instance pool controlled by the session, that is, a transient object is converted into a persistent object.
If you need Hibernate to generate the primary key value, Hibernate will generate the id and set it to the PO.
2: The client submits transactions or refreshes the memory.
3: Find the corresponding hbm. xml file based on the model type and the registration of the cfg ing file in cfg. xml.
4: dynamically spell SQL based on the hbm. xml file and model, as follows:
Insert into Table Name (from hbm. xml) (Field Name List (from hbm. xml) values (list of corresponding values (according to hbm. xml gets the value from the input model ))
5: Execute the SQL statement using JDBC and add the value to the database.
6: return the id of this PO.

 

After session. update (UserModel) is called:
1: DO ---> PO: first, search for the object in the hibernate instance pool based on the model's primary key, and then throw an error.
If you don't have the DO ---> PO, Hibernate will put the incoming DO object in the session-controlled instance pool, that is, converting an instantaneous object into a persistent object.
2: The client submits transactions or refreshes the memory.
3: Find the corresponding hbm. xml file based on the model type and the registration of the cfg ing file in cfg. xml.
4: dynamically spell SQL based on the hbm. xml file and model without dirty data check, as shown below:
Update table name (from hbm. xml) set field name (from hbm. xml) = value (retrieve value from input model according to hbm. xml) where Condition
5: Execute the SQL statement using JDBC and modify the value to the database.
 


After session. update (UserModel) is called:

1: first, search for the object in the hibernate instance pool based on the primary key of the model, and use the PO object (used to check dirty data ).
2: The client submits transactions or refreshes the memory.
3: Hibernate checks the dirty data. If no data is modified, it will not perform the following steps.
4: Find the corresponding hbm. xml file based on the model type and the registration of the cfg ing file in cfg. xml.
5. Perform dirty data check dynamically based on the hbm. xml file and model (if dynamic-update is enabled) as follows:
Update table name (from hbm. xml) set field name (from hbm. xml) = value (retrieve value from input model according to hbm. xml) where Condition
6: Execute the SQL statement using JDBC and modify the value to the database.

 


Id generation method is assigned
After session. delete (UserModel) is called:
1: Search for data in the database based on the primary key of the model to ensure the existence of the object, and then place the found object in the memory, if the corresponding object already exists in the hibernate instance pool at this time (Note: The proxy object is not an object), an exception is thrown.
2: If the corresponding object does not exist in the hibernate instance pool at this time, the object will be put into the memory, but will be identified as the object to be deleted, and it will not be used by load and so on.
3: if the object still does not exist, it will be returned directly (note that no exception is thrown at this time ). That is to say, a query statement is executed before the delete operation.
4: the client submits transactions or refreshes the memory.
5: Determine whether the PO to be deleted exists before deletion. Otherwise, you do not need to delete the PO.
6: If you want to delete a file, perform the following steps. Find the corresponding hbm. xml file based on the registration of the ing file in the model type and cfg. xml.
7. dynamically spell SQL statements based on the hbm. xml file and model, as shown below:
Delete from Table Name (from hbm. xml) where primary key = value (from model)
8: Execute SQL statements using JDBC to delete data from the database.

 


Id generation method is not assigned
N after session. delete (UserModel) is called:
1: Find the corresponding object in the hibernate instance pool based on the primary key of the model (Note: The proxy object is not an object), and an exception is thrown if it is found.
2: if there is no corresponding object in the memory, nothing will be done.
3: the client submits transactions or refreshes the memory.
4: Find the corresponding hbm. xml file based on the model type and the registration of the cfg ing file in cfg. xml.
5. dynamically spell SQL statements based on the hbm. xml file and model, as shown below:
Delete from Table Name (from hbm. xml) where primary key = value (from model)
6: Execute SQL statements using JDBC to delete data from the database. If the data does not exist, an exception is thrown.
After session. delete (UserModel) is called:
1: Find the corresponding object in the hibernate instance pool based on the primary key of the model (Note: The proxy object is not an object) and use this object if it is found.
2: If no corresponding object exists in the memory, search in the database to ensure the existence of the object. Put the found object in the memory and it will not be identified as the object to be deleted, can continue to be used by load and so on. The proxy object also needs to search for data in the database.
3: if the object still does not exist, an exception is thrown. That is to say, a query statement may be executed before the delete operation.
4: the client submits transactions or refreshes the memory.
5: Find the corresponding hbm. xml file based on the model type and the registration of the cfg ing file in cfg. xml.
6: dynamically spell SQL based on the hbm. xml file and model, as follows:
Delete from Table Name (from hbm. xml) where primary key = value (from model)
7. Execute SQL statements using JDBC to delete data from the database.





After we call s. load (UserModel. class, "primary key value:
1: search for an object in the first-level cache based on the model type and primary key value.
2: If no result is found, determine whether lazy = true. If yes, generate a proxy object and return it. Otherwise, search for the second-level cache first. If no second-level cache exists, search for the database. If the proxy object is returned, when you first access the non-primary key attribute, you first find the second-level cache, and the second-level cache does not actually find the database.
3: If you need to find the database, you will find the corresponding hbm. xml file based on the registration of the ing file in the model type and cfg. xml.
4: dynamically spell SQL based on the hbm. xml file and model, as follows:
Select Field List (from hbm. xml) from Table Name (from hbm. xml) where primary key = Value
5: Execute the SQL statement using JDBC and query the data from the database to the rs. If not found, an error is returned.
6: return the Model from the result set ---> model.
 
Note: You can execute query statements when the load method is not enabled for transactions.

 

 
After we call s. get (UserModel. class, "primary key value:
1: first, search for the cache based on the model type and primary key value. If a specific object exists, it will be returned. If a proxy object of the object exists (such as the previous data load, but it is not used yet, load generates a proxy object with only the primary key value). Then, search for the database, fill in the specific data in the proxy object, and return the proxy object, of course, this proxy object has fully loaded data at this time, which is no different from the object.
2: If you want to find the database, find the corresponding hbm. xml file based on the registration of the ing file in the model type and cfg. xml.
3: dynamically spell SQL based on the hbm. xml file and model, as follows:
Select Field List (from hbm. xml) from Table Name (from hbm. xml) where primary key = Value
4: Execute the SQL statement using JDBC and query the data from the database to rs. If no value exists, null is returned.
5: return the Model from the result set ---> model.
 
Note: The get method can execute query statements when transactions are not opened.

 

 

After we call q. list:
1: Perform Semantic Analysis on HQL and analyze the model.
2: Find the corresponding hbm. xml file based on the model type and the registration of the cfg ing file in cfg. xml.
3: According to hbm. the xml file and model are used to parse HQL, so as to dynamically convert HQL to the corresponding SQL. (the process from hql ---> SQL is very complicated, not only to distinguish different databases, it also includes automatic optimization of SQL. Here, only simple examples are as follows:
Select Field List (from hbm. xml) from Table Name (from hbm. xml) where Condition
4: Execute the SQL statement using JDBC and query the data from the database to the rs.
5: From the result set ---> Model set (or object array), then return the model set (or object array)
 
Note: The list () method can execute query statements when transactions are not opened.

Author: jinnianshilongnian

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.