Hibernate Architecture

Hibernate is an ORM framework. The blogger has not actually used this framework. Due to the needs of the course exam, I will sort out the entire architecture and make a brief summary. Although this framework has not been used, it will be discussed on paper, but it can be summarized with the experience accumulated in other ORM frameworks.

References

https://www.w3cschool.cn/hibernate/p7a91ie4.html

insert image description here
The picture above is the architecture diagram of Hibernate. The top layer is the JAVA application program, followed by the persistence layer object, the Hibernate framework, the database interface, and the database.

My understanding that the Hibernate framework can configure different databases is based on the fact that the database provides three types of JTA, JDBC, and JNDI unified interfaces. The design idea is to rely on inversion, and the high-level depends on abstract interfaces, not directly on the database. JTA refers to the distributed transaction interface, the JDBC database operation interface, and JNDI provides a general and unified interface for finding and accessing various naming and directory services.

The following introduces the objects in Hibernate

configuration object

Configuration is the first Hibernate object you create in any Hibernate application, and is usually only created during application initialization. It represents a configuration or properties file required by Hibernate. Configuration objects provide two basic components.

  • Database connections : Handled by one or more configuration files supported by Hibernate. These files are hibernate.properties and hibernate.cfg.xml .
  • Class Mapping Setup : This component creates the link between Java classes and database tables.

Configuration has database connection information and information such as the connection between JAVA classes and database tables.

SessionFactory object

The configuration object is used to create a SessionFactory object, which in turn configures Hibernate for the application using the provided configuration file, and allows instantiation of a session object. SessionFactory is a thread-safe object and is used by all threads of the application.

SessionFactory is a heavyweight object so usually it is created at application startup and persisted for later use. Each database requires a SessionFactory object using a separate configuration file. So if you use multiple databases then you have to create multiple SessionFactory objects.

SessionFactory uses Configuration to configure Hibernate, which is a heavyweight object. We use SessionFactory to obtain Session instances.

Session object

A session is the physical connection used to interact with the database. Session objects are lightweight and are designed so that each instantiation requires interaction with the database. Persistent objects are saved and retrieved through the Session object.

Session objects should not be kept open for long periods of time because they are generally not thread-safe, and they should be created and destroyed as needed.

The Session object is used for the physical link to interact with the database.

Transaction object

A transaction represents a unit of work with the database and most RDBMS support transaction functionality. Transactions in Hibernate are handled by the underlying transaction manager and transactions (from JDBC or JTA).

This is an optional object, and Hibernate applications may choose not to use this interface and manage transactions in their own application code.

Transaction is a transaction-related object. It is optional. Instead of using this interface, you can manage transactions in your own application code.

Query object

The Query object uses SQL or Hibernate Query Language (HQL) strings to retrieve data and create objects in the database. A query instance is used to concatenate query parameters, limit the number of results returned by the query, and ultimately execute the query.

The Query object can be used to retrieve data and create objects in the database using SQL or HQL strings.

Criteria object

Criteria objects are used to create and execute rule-oriented query objects to retrieve objects.

(It looks convoluted, but it actually provides a method that allows you to query the data you want without writing native SQL. Query uses SQL or HQL statements, and HQL is actually SQL, but we use native SQL to It is actually very inconvenient to write, such as the problem of variables in the SQL statement, then you can use HQL to make a placeholder, then set the parameters through the method, and then you can guess that this HQL will prevent problems like SQL injection.)

outline version

  • Configuration interface: configure Hibernate, start Hibernate according to it, and create a SessionFactory object
  • SessionFactory interface: initialize Hibernate, act as a proxy for data storage sources, and create Session objects. SessionFactory is thread-safe, which means that its same instance can be shared by multiple threads of the application. It is a heavyweight and second-level cache.
  • Session interface: responsible for saving, updating, deleting, loading and querying objects. It is not thread-safe and avoids multiple threads from sharing sessions. It is a lightweight, first-level cache.
  • Transaction interface: manage transactions.
  • Query and Critical interfaces: Execute database queries.

Guess you like

Origin blog.csdn.net/rglkt/article/details/122280126