Hibernate is an open-source object relationship ing framework. It implements lightweight object encapsulation for JDBC, so that Java programmers can use the object programming thinking to manipulate the database as they wish. It not only provides ing between Java classes and data tables, but also provides data query and recovery mechanisms. Compared with using JDBC and SQL to operate databases manually, Hibernate can greatly reduce the workload of database operations. In addition, Hibernate can use the proxy mode to simplify the process of loading classes, which will greatly reduce the amount of code written to extract data from the database using hibernate QL, this saves development time and development costs. Server Or the application server is well integrated and now supports almost all popular database servers. In fact, Hibernate has become an industrial standard for the Java data persistence layer.
Hibernate is essentially a middleware that provides database services. It has great flexibility, but its architecture is complicated at the same time, providing several different operation modes. In a lightweight system, applications provide JDBC connections and manage transactions on their own. This method uses a minimum subset of hibernate. In a comprehensive solution system, for applications, all underlying JDBC/JTA APIs are abstracted. hibernate takes care of all the details for you.
I. Core classes and interfaces of Hibernate
1. configuration class: it is the entry of Hibernate and is responsible for configuring and starting hibernate. The Hibernate framework loads the configuration file information through the configuration instance, then reads the content of the specified object link ing file and creates the sessionfactory instance.
2. sessionfactory interface: initializes hibernate. A sessionfactory instance corresponds to a data storage source (generally a database ). The application obtains the session instance from sessionfactory. Sessionfactory has the following features:
Thread security: the same sessionfactory instance can be shared by multiple threads of the application.
It is heavyweight: it requires a large cache to store predefined SQL statements and ing metadata.
3. Session interface: the most frequently used interface in hibernate. Session, also known as persistence manager, manages all persistence-related operations, such as storage, update, deletion, and object loading. Session has the following features:
Single thread, non-shared object: The thread is not secure. When designing the software framework, avoid multiple threads sharing the same session instance.
A session instance is lightweight: it does not need to consume too much resources to create or destroy it.
Session has a cache: stores the objects loaded by the current unit of work.
4. Transaction interface: the transaction interface of the Hibernate framework. It encapsulates underlying transaction interfaces, including JDBC APIs and JTA. In this way, the hibernate application can declare the transaction boundary through the consistent transaction interface, which helps the application to be transplanted in different environments and containers.
5. query and criteria interfaces: they are Hibernate query interfaces used to query objects from data storage sources and control the query execution process. Query encapsulates an hql (Hibernate query language) query statement, while the criteria interface completely encapsulates query statements in the string format, which is more object-oriented than query. Criteria is good at executing dynamic queries.
Ii. hibernate's working process:
1. Start hibernate
2. Create a configuration instance: configuration CFG = new configuration (). Configure ();
3. The configuration instance loads the hibernate. cfg. xml file to the memory.
4. The configuration instance loads the ing file (*. HBM. XML) to the memory according to the hibernate. cfg. xml file.
5. The configuration instance constructs a sessionfactory instance: sessionfactory Sf = cfg. buildsessionfactory ();
6. Create a session instance for the sessionfactory instance: Session session = SF. opensession ();
7. Create a transaction instance from the session instance and start the transaction: Transaction Tx = session. begintransaction ();
8. Various methods provided by the session interface for database operations: Session. save (), update (), saveorupdate (), delete (), get (), load (), and createquery.
9. Submit or roll back a transaction: Tx. Commit (), TX. rollback ()
10. Close session: Session. Close ()
Hibernate:
1. It is a middleware that connects Java applications and relational databases.
2. It encapsulates JDBC APIs and is responsible for the persistence of Java objects.
3. It is an ORM tool that can build a ing between an Object-Oriented Domain Model and a relational model.
Object Relation Mapping (ORM) is a ing technology used to complete object models and relational models. It is a technology that persists object data in applications to relational database tables.
Persistence, as its name implies, is to put the cached content in the database to make it persistent. For objects that require persistence, its lifecycle is divided:
(1) transient state (transient): It was just created with the new statement and has not been persisted and is not in the session cache. It does not have an Object Identifier value (primary key value ).
Feature: it is not associated with any session instance. There is no corresponding record in the database.
(2) Persistent: it has been persisted and added to the session cache. Has the Object Identifier value, and may have a corresponding row in the database. Hibernate ensures that in the cache of the same session instance, each record in the database corresponds to only one persistent instance.
Feature: persistent objects are always associated with a session instance. The persistence instance corresponds to the relevant records in the database. When the session clears the cache, it synchronously updates the database based on the attribute data changes of the persistent instance.
Persistence object features:
Stored in the cache of a session case. It can be said that the persistent object is always associated with a session case.
. The persistence object corresponds to the relevant records in the database
When the session clears the cache, it synchronously updates the database based on the attribute changes of the Persistent Object.
The SAVE () method of the session changes the temporary state to the persistent state.
The Update (), saveorupdate (), and lock () Methods of the session change the free state to the persistent state.
(3) removed: if an object is scheduled to be deleted at the end of a session context, it is removed but still in the session cache, until the work unit ends.
The instance should not be reused when it is removed, because it will be immediately deleted from the database once the work unit is completed.
(4) detached: it has been persisted but is not in the session cache. The instance has been associated with a session context, but that context has been disabled. It owns the Object Identifier value and may have a corresponding row in the database.
Feature: it is no longer in the session cache, that is, it is no longer associated with the session. But it has the Object Identifier value.