Hibernate is the solution for object/relational mapping (Orm,object/relational Mapping), which is to map Java objects and object relationships to the relationships between tables and tables in a relational database. It is a bridge between Java applications and relational databases and is responsible for mapping between Java objects and relational data. Hibernate internally encapsulates the operation of accessing the database through JDBC, providing an object-oriented database access API to upper-level applications.
The architecture of Hibernate
Hibernate maps a Java object or persisted object (persistent Object,po) to a table in a database by using a configuration file (Hibernate.cfg.xml) and a mapping file (*.hbm.xml), and then by manipulating the PO, The data in the data sheet to increase, Delete, change, check and other operations.
Hibernate's core components
1. Hibernate configuration file (Hibernate.cfg.xml):
Used primarily to configure database connection parameters, such as database drivers, URLs, user names, passwords, and so on. Hibernate.cfg.xml also defines the list of xxx.hbm.xml mapping files used in its <mapping> child elements
2. mapping file (xxx.hbm.xml):
It is the core file for hibernate to map the relationship between PO and the data table in the database, the relationship between the PO and the database, and the properties of the PO with the table field one by one.
3. Configuration class:
Used to read Hibernate configuration files and generate Sessionfactory objects.
4. Sessionfactory Interface:
The factory that generated the session instance
5. Session Interface:
Used to manipulate the persisted object po. A get (), load (), save (), update (), delete (), and other methods used to load, save, update, and delete the PO are the core interface of Hibernate
6. Query interface:
Query is responsible for executing various database queries. It can query the PO using the HQL language. The query object can be generated using the CreateQuery () method of the session
7. Transaction interface:
Used to manage hibernate transactions, the main methods are commit () and rollback (), which can be generated from the session's BeginTransaction () method
8. Persistent object (PO):
Can be a normal javabean, each data table corresponding to a persistent object (PO), PO can be considered as a database table mapping Java object, PO should not contain any manipulation of data, the only special is that they are associated with the session.
There are three states of JavaBean in Hibernate:
(1) Temporary status (transient):
Open memory space by the new command the Java object just generated is in a temporary state. Temporary objects are orphaned in memory and do not have any association with the data in the database. In Hibernate, the staging object is associated with the database through the session's save () or Saveorupdate () method, and the data is inserted into the database, which is then transformed into a persisted object.
(2) Persistent State (persistent):
Objects that are in a persistent state are also known as PO, and objects in that state have corresponding records in the database and have a persistent identity. If you use Hibernate's Delete () method, the corresponding persistent object becomes a temporary object, because the corresponding data in the database has been deleted, and the object is no longer associated with the database's records.
When a session executes close () or clear (), the persisted object becomes a de-tube object.
Features of Persistent Objects: association with session instances, and records associated with them in the database.
(3) Off-tube status (detached):
When the session associated with a persisted object is closed, the persisted object is turned into a de-tube object. When the off-pipe object is re-associated to the session, it is converted again into a persistent object.
The de-tube object has the identity value of the database and can be transformed into a persistent object by means of update (), saveorupdate (), etc.
Hibernate's Running Process
(1) The application calls the configuration class, which reads the information from the Hibernate profile-level mapping file and generates a sessionfactory
(2) Generating a Session object from the Sessionfactory object and generating the transaction object with the Session object
(3) loading, saving, updating and deleting po by means of get (), load (), save (), update (), delete (), saveorupdate (), etc. of the Session object ; Generate a query object from the session object for querying operations
(4) If there is no exception, the transaction object will submit the results of these operations to the database
Getting Started with hibernate