Home  >  Article  >  Java  >  A brief analysis of the application summary of hibernate in java programs

A brief analysis of the application summary of hibernate in java programs

高洛峰
高洛峰Original
2017-01-23 11:37:101249browse

We know that if we use java to connect to the database, there is a lot of SQL code, and we want to use a tool to operate the database.
The first thing we think of is JDBC, but if we want to better operate on the attributes of the class, we Will choose Hibernate.

So what is the principle of Hibernate?
hibernate can be understood as a middleware, which is responsible for receiving the sql statements of the java program and sending them to the database.
After hibernate receives the information returned from the database Directly generate an object and pass it to java.

1 First, let’s take a look at the 7 working processes of hibernate:
1. Read and parse the configuration file
2. Create SessionFactory
3. Open Session
4. Start A thing
5. Persistence operation
6. Submit transaction
7. Close Session

Second, we need to understand that the life cycle of the entity object is the three states of Hibernate
Instantaneous State (Transient)
Just created with the new statement, it has not been persisted and is not in the Session cache
Persistent State (Persistent)
Has been persisted and added to the Session cache
Free state (Detached)
has been persisted, but is no longer in the Session cache

A brief analysis of the application summary of hibernate in java programs

3 Persistence and ORM are also parts of Hibernate that need to be understood .
Persistence: Data model (memory) "--" Storage model (persistence device), persistence in Java usually refers to using SQL to store data in a relational database.
ORM is a persistence solution that mainly maps the object model and the relational database relational model, and uses metadata to describe these mappings.

Hibernate must build a project framework to write hibernate configuration files and entity mapping files
Create projects and import jar packages
Create Hibernate configuration files
Used to configure database connections
Run time Various required attributes

The default file name is "hibernate.cfg.xml"
Test connection
Use Hibernate to complete data addition, deletion and modification operations
1. Create entity class
2 .Create and configure mapping files
3.Primary key mapping
4.Primary key generation strategy
5.Add entity mapping file path in the main configuration file
6.Database operations
7.Single table Add, delete, modify and check

Four Hibernate supports two main query methods
(1)HQL (Hibernate Query Languge, Hibernate query language) query
is an object-oriented query language, which does not have tables and field concepts, only the concepts of classes, objects and attributes
HQL is a more widely used method.

(2) Criteria Query
Also known as "object query", it encapsulates the process of constructing a query in an object-oriented manner.

5 When using Hibernate, we should pay attention to some things
(1) How does Hibernate delay loading?
1. Hibernate delayed loading implementation: a) Entity object b) Collection (Collection)
2.Hibernate3 provides the lazy loading function of attributes
When Hibernate is querying data, the data does not exist in the memory. When the program actually operates on the data, the object only exists in the memory, and this is achieved Lazy loading saves the memory overhead of the server, thereby improving the performance of the server.

(2) Let’s talk about Hibernate’s caching mechanism
1. The internal cache exists in Hibernate and is also called the first-level cache, which belongs to the application transaction-level cache
2. Second-level cache:
a) Application and Caching
      b) Distributed Cache
Conditions: Data will not be modified by a third party, data size is within an acceptable range, data update frequency is low, the same data is frequently used by the system,
       Non-critical data
C) The implementation of the third party cache

For more shallow analysis of the application of hibernate in the Java program, please follow the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn