We know that if we use Java to connect to a database, a lot of SQL code, we want to manipulate the database with a tool,
The first thing we think of is JDBC, but we want to do better with the attributes of the class, we will choose Hibernate.
So what is the hibernate principle?
Hibernate can be understood as a middleware it is responsible for the Java program to receive SQL statements sent to the database,
And the information returned by the database hibernate received directly after receiving the object passed to Java.
First, let's take a look at Hibernate's work 7 processes:
1. Read and resolve configuration files
2. Create Sessionfactory
3. Open session
4. Start a thing
5. Persistent operation
6. Submission of services
7. Close session
Second, we want to understand that the life cycle of entity objects is three states of hibernate
Instantaneous state (Transient)
has just been created with the new statement and has not been persisted and is not in the session cache
Persistent State (persistent)
has been persisted and added to the session's cache
Free State (Detached)
has been persisted, but is no longer in the session's cache
Three persistence and ORM are also part of the hibernate that needs to be understood.
Persistence: Data Model (Memory) "-" Storage model (persistent device), which is said to be persistent in Java, usually refers to the use of SQL to store data in a relational database.
ORM is a persistent solution that maps the object model and relational database relational models, and describes them using meta data.
Hibernate must build the project framework to write Hibernate configuration files, entity mapping files
To create a project and import a jar package
Creating a Hibernate configuration file
Used to configure a database connection
Various properties that are required at run time
The default file name is "Hibernate.cfg.xml"
Test connection
Use hibernate to complete data deletion and modification operation
1. Create entity classes
2. Create and configure mapping files
3. Primary KEY Mapping
4. Primary key generation Policy
5. Add the Entity mapping file path to the main configuration file
6. Database Operations
7. Change and check of single watch
Four Hibernate support two main ways of querying
(1) HQL Hibernate Query languge,hibernate Query Language
is an object-oriented query language in which there is no concept of tables and fields, only the concepts of classes, objects, and attributes
HQL is a more widely used approach.
(2) Criteria query
Also known as an object query, it encapsulates the process of constructing queries in an object-oriented manner.
When using hibernate, we should pay attention to some things
(1) How is hibernate delayed loading?
1.Hibernate deferred load implementation: a) collection of entity object B (Collection)
2.hibernate3 provides deferred loading of properties
When the hibernate is querying the data, the data does not exist in memory, when the program is actually operating on the data, the object exists and in memory, it realizes the delay loading, saves the memory cost of the server, and improves the performance of the server.
(2) Next Hibernate's caching mechanism
1. Internal cache exists in the hibernate is also called the first level cache, belongs to the application thing level cache
2. Level Two cache:
a) application and caching
b) Distributed caching
Conditions: The data will not be modified by the third party, the data size in the acceptable range, the data update frequency is low, the same data is frequently used by the system,
Non-critical data
( c) Implementation of third party caching