Hibernate (5)-core hibernate interfaces and working principles

Source: Internet
Author: User
Hibernate core interface

Hibernate has five core interfaces: Session transaction query sessionfactoryconfiguration. These five interfaces constitute the basic elements for running Hibernate and can perform operations such as access, persistence, and transaction management. These five interfaces can be located at the business logic layer and persistence layer of the system. The following is a relationship diagram of hibernate:


 

Session interface:

The session interface is the most important interface for hibernate developers. However, in hibernate, the instantiated session is a lightweight class, and it does not occupy a lot of resources when it is created or destroyed. This is really important in the actual project, because in the client program, the session object may be constantly created and destroyed. If the session overhead is too large, it will bring adverse effects to the system. However, the session object is non-thread-safe. Therefore, in your design, it is best for a thread to create only one session object. Session can be seen as an intermediate interface between data connection and transaction management. We can think of a session as a buffer for persistent objects. hibernate can detect changes to these persistent objects and refresh the database in time. We sometimes call session a persistent layer manager because it contains some persistence layer-related operations, such as storing persistent objects to the database and obtaining them from the database. It should be noted that the hibernate session is different from the httpsession In the JSP application. When we use the term session, we refer to the session in hibernate, And we will later call the httsemi sion object a user session.

 

Sessionfactory interface:

Sessionfactroy interface initializes hibernate. It acts as a proxy for the data storage source and is responsible for creating session objects. The factory model is used here. Note that sessionfactory is not lightweight, because generally, only one sessionfactory is required for a project. When you need to operate multiple databases, you can specify a sessionfactory for each database.

 

Transaction Interface

The transaction interface is responsible for transaction-related operations. This interface is generally used in addition, deletion, and modification of hibernate. However, Hibernate users generally use spring to manage transactions.

 

Query interface

Query is responsible for executing various database queries. It can be expressed in hql or SQL statements. Its return value is generally list. You need to convert it yourself.

 

Configuration interface:

The configuration object is used to configure and start hibernate at the root. The Hibernate application uses the configuration instance to specify the location of the object-link ing file or dynamically configure the hibernate attributes, and then create the sessionfactory instance. We can view the source code of configuration. Its configure () method is implemented as follows:

Public configuration configure () throwshibernateexception {configure ("/hibernate. cfg. xml"); // the location of the orm file is specified here. Return this ;}

We can see that it specifies the location of the ORM file here, which is why hibernate always goes down to classpath by default to find the hibernate. cfg. xml file. In fact, we can also use configure (string resource) to dynamically specify the configuration file, but we usually use the default settings. In this way, our configuration files are read, and the ing files introduced by the <mapping> element in the configuration file are also read.

 

Hibernate running process:

1. Read and parse the hibernate. cfg. xml configuration file through configuration (). Configure ();

2. The ing information is read and parsed by <mappingresource = "com/XX/user. HBM. xml"/> In hibernate. cfg. xml.

3. Create sessionfactory through config. buildsessionfactory (); //

4. sessionfactory. opensession (); // open sesssion

5. session. begintransaction (); // create transaction Transation

6. Persistent operate persistent operation // This method is generally used to save

7. session. gettransaction (). Commit (); // submit the transaction

8. Disable session

9. Disable sesstionfactory

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.