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

What are the core interfaces of Hibernate

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what are the core interfaces of Hibernate". In daily operation, I believe many people have doubts about the core interfaces of Hibernate. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "what are the core interfaces of Hibernate?" Next, please follow the editor to study!

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. But it's worth noting that Session objects are not thread-safe, so in your design, * is a thread that creates 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 persistence 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 call the HttpSesion object 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 the actual transaction implementation, which includes transactions in JDBC, 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.

The policy interface Hibernate differs from some other open source software in that it is highly extensible, which is achieved through its built-in policy mechanism. When you feel that some of the functions of Hibernate are inadequate or there are some defects, you can develop your own policy to replace it, and all you have to do is inherit one of its policy interfaces, and then implement your new strategy. Here are its policy interfaces:

Generation of ◆ primary key (IdentifierGenerator interface)

◆ native SQL language support (Dialect abstract classes)

◆ buffering mechanism (Cache and CacheProvider interfaces)

◆ JDBC connection Management (ConnectionProvider Interface)

◆ transaction management (TransactionFactory, Transaction, and TransactionManagerLookup interfaces)

◆ ORM Policy (ClassPersister interface)

◆ attribute access Policy (PropertyAccessor Interface)

Creation of ◆ proxy object (ProxyFactory interface) Hibernate creates a default implementation for each of the mechanisms listed above, so if you just want to enhance the functionality of one of its strategies, simply inherit the class, there is no need to write code from scratch.

At this point, the study of "what are the core interfaces of Hibernate" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report