Summary of hibernate application in Java program

  • 2020-04-01 02:09:37
  • OfStack

We know that if we're using Java to connect to a database, a lot of SQL code, we want to use a tool to manipulate the database,
Our first thought was JDBC, but to better manipulate the class's properties, we chose Hibernate.

So what is the Hibernate principle?
Hibernate can be understood as a piece of middleware that takes SQL statements from Java programs and sends them to the database,
Hibernate receives the information returned from the database and generates an object directly to Java.

A   First, let's look at the 7 processes of hibernate:
1. Read and parse the configuration file    
2. Create a SessionFactory
3. Open the Session
Start something
5. Persist operations
6. Commit transactions
7. Close the Session

Second, we need to understand that the lifecycle of an entity object is the three states of Hibernate
Transient state (Transient)
Just created with a new statement, not persisted, and not in Session cache
Persistent state (Persistent)
It is persisted and added to the Session cache
The free state (Detached)
It's persisted, but it's no longer in the Session cache
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201307/20130721171427937.jpg ">

Three   Persistence and ORM are also part of Hibernate's understanding.
Persistence: data model (memory) -- storage model (persistence device). In Java, persistence usually refers to the use of SQL to store data in a relational database.
ORM is a persistence solution that maps the object model to the relational database relational model and describes these mappings using metadata.

Hibernate must build a project framework to write Hibernate configuration files, entity mapping files
Create the project and import the jar package
Create a Hibernate configuration file    
Used to configure a database connection
Various properties required at run time

The default file is called "hibernate.cfg.xml"
Test the connection
Use Hibernate to complete the operation of adding, deleting and changing data
1. Create entity classes
2. Create and configure the mapping file
3. Primary key mapping
4. Primary key generation strategy
5. Add the entity mapping file path to the main configuration file
6. Database operation
7. Add, delete, change and check a single table

Hibernate supports two main query modes
(1)HQL (Hibernate Query language, Hibernate Query language)
The query
Is an object-oriented query language, in which there is no concept of tables and fields, only the concept of classes, objects and properties
HQL is the more widely used approach.

(2) the Criteria query
Also known as "object query", it USES an object-oriented way to construct the process of query to do encapsulation.

When using Hibernate, we should pay attention to a few things
(1) how does Hibernate lazily load?
      1.Hibernate lazy load implementation: a) entity object b) Collection
      2.Hibernate3 provides lazy loading of properties
When Hibernate queries data, the data does not exist in memory. When the program actually operates on the data, the object exists in memory, which realizes lazy loading. It saves the memory overhead of the server and thus improves the performance of the server.

(2) talk about Hibernate's caching mechanism
      1. Internal cache exists in Hibernate, also called first-level cache, which belongs to the application event-level cache
      2. Level 2 cache:  
              A) application and caching  
              B) distributed cache  
      Conditions: the data will not be modified by the third party, the data size is 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 cache


Related articles: