Hibernate's running process

Through  the study of the tutorial  " Hibernate realizes the function of adding, deleting, modifying and checking ", readers  have already had a preliminary understanding of the use of Hibernate .

In the Hibernate program in the tutorial "  Hibernate Realizes Addition, Deletion, Modification and Checking Function ", it mainly involves the use of four interfaces, namely Configuration interface, SessionFactory interface, Session interface and Transaction interface. In addition to these four interfaces, the commonly used interfaces include Query and Criteria et al. The details of these interfaces will be explained in the following tutorials. This section will explain the operating mechanism of Hibernate.

The execution flow of Hibernate at runtime is shown in Figure 1.

Hibernate execution flow


Figure 1 Hibernate execution process


The execution process of Hibernate shown in Figure 1 is as follows.

1) Create a Configuration instance, load the Hibernate core configuration file and mapping file information into the Configuration object.

2) Create an instance of SessionFactory. Create a SessionFactory object through the configuration file information read by the Configuration object, which stores the configuration information of the current database and all mapping relations and other information.

3) Create a Session instance and establish a database connection. Session is mainly responsible for adding, deleting, modifying, and checking operations of persistent objects. Creating a Session is equivalent to creating a new database connection.

4) Create a Transaction instance and start a transaction. Transaction is used for transaction management, and a transaction corresponding to a Transaction object can contain multiple operations. When using Hibernate to add, delete, and modify operations, you must first create a Transaction object. It should be noted that Hibernate's transactions are closed by default, and transactions need to be manually opened and closed.

5) Use the various methods passed by the Session interface to perform persistent operations.

6) Submit the transaction. After the entity object is persisted, the transaction must be submitted.

7) Close the Session and SessionFactory, and disconnect from the database.

Note: Session in Hibernate is different from   HttpSession in JSP . When using the Session object in Hibernate, it usually refers to the Session in Hibernate, and HttpSession is called the user session Session.

 

Guess you like

Origin blog.csdn.net/unbelievevc/article/details/132096074