Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Several interfaces in hibernate

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Session interface

The Session interface is one of the most important interfaces for Hibernate developers. In Hibernate, however, the instantiated Session is a lightweight class, and it doesn't take a lot of resources to create and destroy it. This is really important in real-world projects, because in client programs, Session objects may be constantly created and destroyed, and if the cost of Session is too high, it will adversely affect the system. It is worth noting, however, that Session objects are not thread-safe, so in your design, it is best for a thread to create only one Session object.

In the minds of the designers of Hibernate, they see session as an intermediate interface between data connection and transaction management. We can think of session as a buffer of persistent objects, and Hibernate can detect changes to these persistent objects and refresh the database in time. We sometimes call Session a persistence layer manager because it contains these persistent layer-related operations, such as storing persistent objects to the database and getting them from the database. Note that the session of Hibernate is different from HttpSession in JSP applications. When we use the term session, we refer to the session in Hibernate, and we will later refer to the HttpSesion object as the user session.

SessionFactory interface

A design pattern, the factory pattern, is used here, and the user program gets an instance of Session from the factory class SessionFactory.

What makes you feel strange is that SessionFactory is not lightweight! In fact, the intention of its designers is to make it shared throughout the application. Typically, only one SessionFactory is needed for a project, but when your project operates on multiple databases, you must specify a SessionFactory for each database.

SessionFactory actually acts as a buffer in Hibernate, buffering SQL statements automatically generated by Hibernate and some other mapping data, as well as some data that may be reused in the future.

Configuration interface

The purpose of the Configuration interface is to configure Hibernate and start it. During the startup of Hibernate, an instance of the Configuration class first locates the mapping document, reads these configurations, and then creates a SessionFactory object.

Although the Configuration interface plays only a small role in the entire Hibernate project, it is every object you encounter when you start hibernate.

Transaction interface

The Transaction interface is an optional API, and you can choose not to use it and replace it with the underlying transaction code written by the designers of Hibernate. The Transaction interface is an abstraction of actual transaction implementations, including JDBC transactions, UserTransaction in JTA, and even CORBA transactions. The reason for this design is to enable developers to use a unified transaction interface, so that their projects can easily move values between different environments and containers.

Query and Criteria interface

The Query interface allows you to easily query databases and persistent objects, which can be expressed in two ways: HQL or SQL statements in the local database. Query is often used to bind query parameters, limit the number of query records, and finally perform query operations.

The Criteria interface is very similar to the Query interface in that it allows you to create and execute standardized object-oriented queries.

It is worth noting that the Query interface is also lightweight and cannot be used outside of Session.

Callback interface

When some useful events occur, such as the loading, storage, and deletion of persistent objects, the Callback interface tells Hibernate to receive a notification message. In general, the Callback interface is not required in user programs, but you may use it when you want to create audit logs in your project.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report